|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to cycle through checkboxes in a datagrid and find out which ones are checked?that can cycle through them and associate them with each row in the data grid. I was able to add the checkbox and I can see them, but I can't get a handle on any of them when I cycle through them. Here's what I have to add the checkbox. <asp:templatecolumn HeaderText="Select"> <itemtemplate> <asp:checkbox id="MyCheckbox" runat="Server" Enabled="true" /> </itemtemplate> </asp:templatecolumn> This "seems" to work because I can see the one checkbox for each row in the data grid. To test it out, I added a button and on the click, cycle through them and try to find ones that are checked: Viewing source of the datagrid web page, here's what I see: <td>Excel</td><td> <a id="dgTitles__ctl4_hl" href="readcode.aspx?CodeID=305">Unique Count</a> </td><td>11/16/2005</td><td>11/16/2005</td><td>2</td><td> <input id="dgTitles__ctl4_MyCheckbox" type="checkbox" name="dgTitles:_ctl4:MyCheckbox" /> </td> So I added some code to try to get a handle on them. I got this from google and tried to modify it. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim dg As DataGrid = dgTitles Dim dgi As DataGridItem Dim s As String Dim ctl As CheckBox Dim i As Integer For Each dgi In dg.Items s = dgi.ClientID & "_MyCheckbox" ctl = CType(dgi.FindControl(s), CheckBox) If Not (ctl Is Nothing) Then If ctl.Checked Then i += 1 End If End If Next Response.Write("total checked=" & i) End Sub No matter how I try to reference the checkbox, I can't get a handle on it. How do I cycle through the checkboxes and find out which ones are checked? thanks Hi,
try out the code like this. here the checkbox ids i have mentioned in the template column is chkselect. foreach(DataGridItem oDataGridItem in dgrd.Items) { chkselectASPx=(CheckBox) oDataGridItem.FindControl("chkSelect"); if(chkselectASPx.Checked) { } else { } } -- Show quoteHide quoteS.Sundararajan "Tim Zych" wrote: > I am trying to add a repeating checkbox to a datagrid and have some code > that can cycle through them and associate them with each row in the data > grid. > > I was able to add the checkbox and I can see them, but I can't get a handle > on any of them when I cycle through them. > > Here's what I have to add the checkbox. > > <asp:templatecolumn HeaderText="Select"> > <itemtemplate> > <asp:checkbox id="MyCheckbox" runat="Server" Enabled="true" /> > </itemtemplate> > </asp:templatecolumn> > > This "seems" to work because I can see the one checkbox for each row in the > data grid. > > To test it out, I added a button and on the click, cycle through them and > try to find ones that are checked: > > Viewing source of the datagrid web page, here's what I see: > > <td>Excel</td><td> > <a id="dgTitles__ctl4_hl" href="readcode.aspx?CodeID=305">Unique Count</a> > </td><td>11/16/2005</td><td>11/16/2005</td><td>2</td><td> > <input id="dgTitles__ctl4_MyCheckbox" type="checkbox" > name="dgTitles:_ctl4:MyCheckbox" /> > </td> > > So I added some code to try to get a handle on them. I got this from google > and tried to modify it. > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button1.Click > Dim dg As DataGrid = dgTitles > Dim dgi As DataGridItem > Dim s As String > Dim ctl As CheckBox > Dim i As Integer > For Each dgi In dg.Items > s = dgi.ClientID & "_MyCheckbox" > ctl = CType(dgi.FindControl(s), CheckBox) > If Not (ctl Is Nothing) Then > If ctl.Checked Then > i += 1 > End If > End If > Next > Response.Write("total checked=" & i) > End Sub > > No matter how I try to reference the checkbox, I can't get a handle on it. > > How do I cycle through the checkboxes and find out which ones are checked? > > thanks > > > That works.
Here's the VB version: For Each dgi In dg.Items ck = CType(dgi.FindControl("MyCheckBox"), CheckBox) If Not (ck Is Nothing) Then If ck.Checked Then i += 1 End If End If Next Thanks! Show quoteHide quote "Sundararajan" <sundarara***@discussions.microsoft.com> wrote in message Count</a>news:342CD51E-3BEF-47F1-B659-A30CAD1B4E7F@microsoft.com... > Hi, > > try out the code like this. here the checkbox ids i have mentioned in the > template column is chkselect. > > foreach(DataGridItem oDataGridItem in dgrd.Items) > { > > chkselectASPx=(CheckBox) oDataGridItem.FindControl("chkSelect"); > if(chkselectASPx.Checked) > { > } > else > { > } > } > > -- > S.Sundararajan > > > "Tim Zych" wrote: > > > I am trying to add a repeating checkbox to a datagrid and have some code > > that can cycle through them and associate them with each row in the data > > grid. > > > > I was able to add the checkbox and I can see them, but I can't get a handle > > on any of them when I cycle through them. > > > > Here's what I have to add the checkbox. > > > > <asp:templatecolumn HeaderText="Select"> > > <itemtemplate> > > <asp:checkbox id="MyCheckbox" runat="Server" Enabled="true" /> > > </itemtemplate> > > </asp:templatecolumn> > > > > This "seems" to work because I can see the one checkbox for each row in the > > data grid. > > > > To test it out, I added a button and on the click, cycle through them and > > try to find ones that are checked: > > > > Viewing source of the datagrid web page, here's what I see: > > > > <td>Excel</td><td> > > <a id="dgTitles__ctl4_hl" href="readcode.aspx?CodeID=305">Unique Show quoteHide quote > > </td><td>11/16/2005</td><td>11/16/2005</td><td>2</td><td> > > <input id="dgTitles__ctl4_MyCheckbox" type="checkbox" > > name="dgTitles:_ctl4:MyCheckbox" /> > > </td> > > > > So I added some code to try to get a handle on them. I got this from > > and tried to modify it. > > > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > > System.EventArgs) Handles Button1.Click > > Dim dg As DataGrid = dgTitles > > Dim dgi As DataGridItem > > Dim s As String > > Dim ctl As CheckBox > > Dim i As Integer > > For Each dgi In dg.Items > > s = dgi.ClientID & "_MyCheckbox" > > ctl = CType(dgi.FindControl(s), CheckBox) > > If Not (ctl Is Nothing) Then > > If ctl.Checked Then > > i += 1 > > End If > > End If > > Next > > Response.Write("total checked=" & i) > > End Sub > > > > No matter how I try to reference the checkbox, I can't get a handle on it. > > > > How do I cycle through the checkboxes and find out which ones are checked? > > > > thanks > > > > > >
Other interesting topics
VS2005 C# is very BUGGY, please hlp
custom control client values gone with postback Server Control - object not saved when button clicked repost: custom control client values gone with postback -- no solution found... Setting focus to TextBox DropDownList Value Mouseover, mouseout, click etc on datagrid. DataList Issue Maintaining "id" attribute value in server controls What is the Equivalent of the CurrentRowIndex Property for a DataGrid Web Control? |
|||||||||||||||||||||||