Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.Net Loop through Checkboxlist items

Posted on 2011-09-16
3
Medium Priority
?
1,772 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 500 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 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