Solved

VB.Net Loop through Checkboxlist items

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

Technology Partners: 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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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