Solved

VB.Net Loop through Checkboxlist items

Posted on 2011-09-16
3
1,502 Views
Last Modified: 2012-05-12
I'm using a checkboxlist nested inside a GridView control and cannot seem to get the For Each loop to run code beyond the first item in the checkboxlist.  Each row of the GridView has a button designed to execute the row command for the selected/unselected items in the checkboxlist.  No matter how many items are in the checkboxlist the loop only runs for the first item in the list.  Any ideas on what I'm missing here?  Thanks

Protected Sub GridViewItems_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridViewItems.RowCommand

         Select e.CommandName
            Case "CMD_Save"
                Dim strSQL As String
                Dim index = Convert.ToInt32(e.CommandArgument)
                Dim row = Me.GridViewItems.Rows(index)

                Dim chk As CheckBoxList = TryCast(row.FindControl("CheckBoxListItems2"), CheckBoxList)

                For Each Item In chk.Items
                    Dim ItemName As String = Item.Value
                    Dim Checked As Boolean = Item.selected

                    If Checked = True Then
                        strSQL = "exec sp_CheckedRecords "  & ItemName
                        'prodPoster.updateManufacturing(strSQL)
                        Page.ClientScript.RegisterStartupScript(Me.GetType, "Temp", "alert('" & strSQL & "');", True)
                    End If

                    If Checked = False Then
                        strSQL = "exec sp_UnCheckedRecords "  & ItemName
                        'prodPoster.updateManufacturing(strSQL)
                        Page.ClientScript.RegisterStartupScript(Me.GetType, "Temp", "alert('" & strSQL & "');", True)
                    End If
                Next
        End Select
End Sub

Open in new window

0
Comment
Question by:Eutectic
3 Comments
 
LVL 12

Accepted Solution

by:
jagssidurala earned 125 total points
ID: 36553083
Your code seems to be correct.

But write try catch block in ur row command event as i am thinking that you are getting error while reading the check box items.

or else

are you getting any error........
0
 
LVL 1

Assisted Solution

by:rrvenki
rrvenki earned 125 total points
ID: 36554185
Protected Sub MyBtn_Click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ButtonCommandEventArgs) Handles Button.Click  

For Each iDGI As DataGridItem In MyDataGrid.Items
	Dim myCheckbox As CheckBox = DirectCast(MyDataGrid.Cells(0).Controls(1), CheckBox)
	If myCheckbox.Checked = True Then
		rowCount += 1
                                           'Do what you wish with current row -> iDGI
	End If
Next

End Sub

Open in new window

0
 

Author Closing Comment

by:Eutectic
ID: 36560477
My code was working fine just the problem was using the java script pop up to validate sql code I wanted to send.  I was only seeing it pop up once.  I added the rowcount integer and moved the popup out side for each loop and the row counts came out correctly.  Lesson learned is not to use java script popups in this manner.  Thanks for the help.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question