Click here to Skip to main content
15,936,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following HTML Page with the checkbox "chkZiel" in the gridview "dgZiel":
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="CC_SimpleTargetReport.aspx.cs" Inherits="CCMatrixDB_WEB.REPORT.CC_SimpleTargetReport" %>

<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="Scripts/JavaSript_scroll.js"></script>
    <asp:Table ID="Table1" runat="server" Width="100%">
        <asp:TableRow ID="TableRow1" runat="server">
            <asp:TableCell ID="TableCell1" runat="server" HorizontalAlign="Left">
                <asp:TextBox ID="tbCOL1" runat="server" BorderWidth="0" BorderStyle="None" Text="TARGET" Width="100"></asp:TextBox>
            <asp:TableCell ID="TableCell2" runat="server" HorizontalAlign="Left">
                <asp:TextBox ID="tbCOL2" runat="server" BorderWidth="0" BorderStyle="None" Text="COMPONENT" Width="100"></asp:TextBox>
        <asp:TableRow ID="TableRow2" runat="server">
            <asp:TableCell ID="TableCell3" runat="server" HorizontalAlign="Left">
                <DIV style=" border-style: double; OVERFLOW: auto; WIDTH: 550px; HEIGHT: 168px"> 
                    <asp:GridView ID="dgZiel" runat="server" BackColor="White" 
                        CellPadding="4" GridLines="Horizontal" AutoGenerateColumns="false" ShowHeader="False">
                            <asp:TemplateField ItemStyle-HorizontalAlign="Center" >
                                    <asp:CheckBox ID="chkZiel" runat="server"  Width="40px" />
                            <asp:BoundField DataField="Name" ItemStyle-Width="100%" />
		<asp:TableRow ID="TableRow3" runat="server">
            <asp:TableCell ID="TableCell5" runat="server" HorizontalAlign="Left" ColumnSpan="2" VerticalAlign="Bottom" Height="45px">
                <asp:Button ID="Button" runat="server" Text="GENERATE REPORT" Width="142px" onclick="Button_Click" />

The following code checks the state of the checkbox for all gridview rows:

    protected void Button_Click(object sender, EventArgs e)
        for (int i = 0; i < dgZiel.Rows.Count; i++)
            CheckBox chkZiel = (CheckBox) dgZiel.Rows[i].FindControl("chkZiel");
            if (chkZiel.Checked == true)
                string name = dgZiel.Rows[i].Cells[1].Text;

But the state of all checkboxes are uncheced (chkZiel.Checked -> false) equal if the checkbox is checked or not.
What is wrong?

What I have tried:

I tried different c# codes, but the state of the checkbox stays always on unchecked.
Updated 20-Jun-16 4:12am
Kornfeld Eliyahu Peter 20-Jun-16 9:28am    
You button click initiates a full post back of the page...All controls initialized to their original state (unchecked for check-box)...
Go and learn about partial post back - AJAX...
Karthik_Mahalingam 20-Jun-16 9:30am    
post your databind code

Thank you wery much. I forget the:
if (!this.IsPostBack)
statement in the Page_Load() function.
Share this answer
Vincent Maverick Durano 21-Jun-16 14:45pm    
That's what I've said. :) Glad it works for you now :)
This will typically happen if you are not binding your GridView within Not Ispostack block. If you are binding your Grid on page load then make sure to wrap it within Not Ispostback. For example:

protected void Page_Load(object sender, EventArgs e){
        if (!IsPostBack)
            // Populate your grid here and call DataBind()

This will ensure that the state of your CheckBox will not reset on postbacks.
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