0

aspx code

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="TestButton" runat="server" OnClick="TestButton_Click"
            Text="AddNewRow" />
    </div>
    <div>
        <asp:GridView ID="MyGrid" runat="server" GridLines="Both"
            AutoGenerateColumns="false" OnDataBound="MyGrid_DataBound"
            EnableViewState="true">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="name" />
                <asp:TemplateField HeaderText="CheckBox1">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" EnableViewState="true"
                           runat="server" AutoPostBack="true" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CheckBox2">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox2" runat="server"
                           EnableViewState="true" AutoPostBack="true" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CheckBox3">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox3" runat="server"
                           AutoPostBack="true" EnableViewState="true" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code behind

public DataTable Dt = new DataTable();
protected void Page_Load(object sender, EventArgs e) { }
protected void TestButton_Click(object sender, EventArgs e)
{
   DataRow dr = null;
   if(ViewState["T"]==null)
   {
      Dt.Columns.Add(new DataColumn("Name", typeof(string)));
      Dt.Columns.Add(new DataColumn("CheckBox1", typeof(bool)));
      Dt.Columns.Add(new DataColumn("CheckBox2", typeof(bool)));
      Dt.Columns.Add(new DataColumn("CheckBox3", typeof(bool)));
      ViewState["T"] = Dt;
    }
    Dt =(DataTable) ViewState["T"];
    dr = Dt.NewRow();
    dr["Name"] = "Sam";
    dr["CheckBox1"] = true;
    dr["CheckBox2"] = false;
    dr["CheckBox3"] = false;
    Dt.Rows.Add(dr);
    SearchGrid.DataSource = Dt;
    SearchGrid.DataBind();
 }
 protected void MyGrid_DataBound(object sender, EventArgs e)
 {
    (SearchGrid.Rows[SearchGrid.Rows.Count - 1].Cells[1].FindControl("CheckBox1")
                as CheckBox).Checked = true;
  }
}

when i click the the Addnew row button the result would be some thing like this

enter image description here

but when i click again

enter image description here

the first row check box gets uncheckd. i have enabled the view state for each checkbox but still it does not work. is tehre some thing i should do with the data table which i am binding?

1 Answer 1

1

Your ViewState is perfectly fine. It must be storing the data you want to persist.

You haven't bound your gridView Columns to any field, in this case the DataColumn names of your DataTable. Bind your checkboxes like this:

<asp:CheckBox ID="CheckBoxX" runat="sever" 
           Checked ='<%# Eval("DataFieldName") %>' />

i.e.   

<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("CheckBox1") %>' 
      AutoPostBack="true" EnableViewState="true" />

<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Eval("CheckBox2") %>' 
  AutoPostBack="true" EnableViewState="true" />

<asp:CheckBox ID="CheckBox3" runat="server" Checked='<%# Eval("CheckBox3") %>' 
  AutoPostBack="true" EnableViewState="true" />

since CheckBox1, CheckBox2, CheckBox3 are the field(column) names of the DataSource assigned to the SearchGrid(GridView)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.