1

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

            }
        }
    }
Saeid Amini
  • 1,313
  • 5
  • 16
  • 26
  • 1
    it has passed a long time since I didn't use WebForms. But I think you need to make sure about the postback problems there, see this https://stackoverflow.com/a/34545012/2946329 – Salah Akbari Jul 10 '19 at 05:32
  • Are you sure to check the CheckBox befor button click? Another important thing is body of Page_Load. If you fill the `GridView` in `Page_Load` you must take `filling` code in `if (!IsPostBack)` . Can you show us body of Page_Load? – Eldaniz Ismayilov Jul 10 '19 at 06:53
  • Hi guys, problem was resolved! Eldeniz - you are right! It was in Page_Load in (!IsPostBack) section. You made my day! :) – Štefan Švestka Jul 10 '19 at 18:57

1 Answers1

0
    Pleae check with below code

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