Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My GridView contains 2 column (UserId and a CheckBox field).

The user is selecting a few rows by checking in the checkBox.
While debugging, it shows false (ie.unchecked) and therefore it is not executing the code within "if (cb.Checked)".

I am not able to find where I did the mistake.
//==========================================================

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false">
        <Columns >
        <asp:TemplateField >
        <ItemTemplate >
        <asp:Label ID="Label1" runat="server" Text='<%#Eval("UserId")%>'></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField >
        <ItemTemplate >
            <asp:CheckBox ID="CheckBox1" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

        <br /><br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

//==========================================================
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)gr.FindControl("CheckBox1");
            if (cb.Checked)
            {
                string aa = "wkwk";
            }
        }
    }
Posted
Updated 19-Apr-11 21:36pm
v2
Comments
Ciumac Sergiu 20-Apr-11 2:23am    
Have you tried (Checkbox)gr[1], instead of using FindControl?
ParthaDinda 20-Apr-11 2:29am    
Are you handel postback? if write any code in page load try to write
in if(!postback) block.
Otherwise please share your full cs page code.
SHAJANCHERIAN 20-Apr-11 3:11am    
Thank You
Dalek Dave 20-Apr-11 3:36am    
Edited for Grammar, Syntax and Readability.

1 solution

I could find the error.
The code was like this.
C#
protected void Page_Load(object sender, EventArgs e)
    {
       //Databinding to GridView

    }

//===================================================================
The corrected code is as follows
//===================================================================
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           //Databinding to Gridview
        }

    }
 
Share this answer
 
Comments
Dalek Dave 20-Apr-11 3:36am    
Good Find.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900