Datagrid & checkbox

Discussion in 'ASP .Net Web Controls' started by Lee Trotter, Aug 15, 2004.

  1. Lee Trotter

    Lee Trotter Guest

    I have a datagrid with a checkbox colum created using a template colum

    How can I reference the checkboxes at run time though code?

    Thanks

    Lee
     
    Lee Trotter, Aug 15, 2004
    #1
    1. Advertising

  2. Hi Lee,

    You need to loop through the datagrid items (rows) and then find the
    checkbox control. Then test if it is checked.

    For Each dgItem In Me.DataGrid1.Items
    chkSelected = dgItem.FindControl("chkSelect")
    If chkSelected.Checked Then
    Label2.Text = Label2.Text & _
    chkSelected.UniqueID & "<br>"
    End If
    Next


    Full source below.

    Does this help?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto


    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    If Not IsPostBack Then
    DataGrid1.DataSource = CreateDataSource()
    DataGrid1.DataBind()
    End If
    End Sub

    Private Sub btnModifier_Click _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles btnModifier.Click
    Dim dgItem As DataGridItem
    Dim chkSelected As CheckBox
    Label2.Text = ""
    For Each dgItem In Me.DataGrid1.Items
    chkSelected = dgItem.FindControl("chkSelect")
    If chkSelected.Checked Then
    Label2.Text = Label2.Text & _
    chkSelected.UniqueID & "<br>"
    End If
    Next
    End Sub

    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add _
    (New DataColumn("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function 'CreateDataSource


    <asp:DataGrid id="DataGrid1" runat="server">
    <Columns>
    <asp:TemplateColumn HeaderText="Boolean Value">
    <ItemTemplate>
    <asp:CheckBox id=chkSelect runat="server" Checked='<%#
    DataBinder.Eval(Container, "DataItem.Boolean") %>' >
    </asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Boolean Value">
    <ItemTemplate>
    <asp:Label id="Label1" runat="server">
    <%# IIF(DataBinder.Eval(Container, "DataItem.Boolean"),"Yes","No")
    %>
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    <P>
    <asp:Label id="Label2" runat="server"></asp:Label></P>
    <P>
    <asp:Button id="btnModifier" runat="server"
    Text="Button"></asp:Button></P>

    "Lee Trotter" <latrotter@@@sympatico.ca> wrote in message
    news:...
    >I have a datagrid with a checkbox colum created using a template colum
    >
    > How can I reference the checkboxes at run time though code?
    >
    > Thanks
    >
    > Lee
    >
    >
     
    Ken Cox [Microsoft MVP], Aug 15, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    0
    Views:
    531
    tshad
    Apr 15, 2005
  2. Vikram

    disable checkbox list checkbox

    Vikram, Jan 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,406
    ThunderMusic
    Jan 25, 2006
  3. =?Utf-8?B?RG90TmV0RGV2?=

    DataGrid and embeded Checkbox..How to find if checkbox clicked

    =?Utf-8?B?RG90TmV0RGV2?=, Oct 6, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    830
    David Wier
    Oct 6, 2006
  4. Jason Huang
    Replies:
    0
    Views:
    558
    Jason Huang
    Jun 29, 2007
  5. Joey Pang
    Replies:
    4
    Views:
    523
    Joey Pang
    Jun 13, 2005
Loading...

Share This Page