Solved

VB.Net Loop through Checkboxlist items

Posted on 2011-09-16
3
1,641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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