Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a gridview with a "checkboxfield" column, but when I press the asp.net button, all the checkboxes are checked in false due to postback.

I want to get the checkboxes values when I click the button in my codeBehind with C#.

Is it possible?

SOLUTION:
My error was to use the "checkboxfield" column in gridview but is better to use the "templatefield" as the solution1 in this post.

ERROR:
ASP.NET
<asp:CheckBoxField DataField="ESTPROopc" HeaderText="OPC"/>


SOLUTION: SOLUTION1 in this post is right.
ASP.NET
<asp:TemplateField HeaderText="OPC">
    <ItemTemplate>
       <asp:CheckBox ID="ESTPROopc" runat="server" Checked='<%#Eval("ESTPROopc")%>'/>
    </ItemTemplate>
</asp:TemplateField>


C#
bool ischecked = ((CheckBox)row.FindControl("ESTPROopc")).Checked;


What I have tried:

C#
CheckBox chkOpc = row.Cells[4].Controls[0] as CheckBox;
bool check = chkOpc.Checked;
Posted
Updated 16-Feb-17 3:16am
v2
Comments
Richard Deeming 15-Feb-17 12:43pm    
Are you re-binding the grid in the Page_Load function without checking the IsPostBack property first?
judah9107 15-Feb-17 13:09pm    
No, look my Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//load GridView
loadEstrucProyectosEjemplo(lstEstrucProyectosEjemplo);
}
}

1 solution

Please check below code, I've tested it.

ASPX CODE:

ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="Title" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" Checked='<%#Eval("CheckValue") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:Button ID="btnGetCheck" runat="server" Text="Get Check" OnClick="btnGetCheck_Click" />


ASPX.CS CODE:

C#
class CheckExample
{
    public string Title { get; set; }
    public bool CheckValue { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<CheckExample> list = new List<CheckExample>();
        list.Add(new CheckExample() { Title = "A", CheckValue = false });
        list.Add(new CheckExample() { Title = "B", CheckValue = false });
        list.Add(new CheckExample() { Title = "C", CheckValue = false });
        list.Add(new CheckExample() { Title = "D", CheckValue = false });
        list.Add(new CheckExample() { Title = "E", CheckValue = false });

        GridView1.DataSource = list;
        GridView1.DataBind();
    }
}

protected void btnGetCheck_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in GridView1.Rows)
    {
        bool chkValue = ((CheckBox)row.FindControl("chk")).Checked;
    }
}
 
Share this answer
 

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