Solved

VB.Net Loop through Checkboxlist items

Posted on 2011-09-16
3
1,469 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now