简体   繁体   中英

How to check if checkbox is checked in gridviewrow

I need help with this code where I check every row in GridView if the CheckBox is checked or not. But there is still false. Can you help me, please?

<asp:GridView ID="GridView1" CssClass="tabulka" runat="server" AutoGenerateColumns="false" />
        <Columns>
            <asp:TemplateField HeaderText="Placení">
                <ItemTemplate>
                    <asp:CheckBox ID="Poslano" runat="server" Text="Vyřešeno"  />
                    <asp:HiddenField ID="id" runat="server" Value='<%# Eval("id").ToString() %>' />
                </ItemTemplate>
            </asp:TemplateField>

         </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    </asp:GridView>





protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvRow in GridView1.Rows)
        {
            if (((CheckBox)gvRow.FindControl("Poslano")).Checked) // here is a problem - still false
            {
                Tabor tab = new Tabor();

                tab.Id = Convert.ToInt32(((HiddenField)gvRow.FindControl("id")).Value); // here is good value

            }
        }
    }
    Pleae check with below code

   foreach(var gvItem in GridView1.Items)
    {
      CheckBox chkItem = (CheckBox) gvItem.FindControl("Poslano");
      if (chkItem.Checked)
     {
       //Do stuff
     }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM