Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have an gridview with template fields which contains checkboxes.I have a button outsode gridview which is called as save button.when i check the checkboes and click on save button then y is storing in database otherwise N.my problem is when i m clicking the save button the checked cheboes becomes unchecked. i want to see the checked check boxes remain checked until i uncheck them. I tried a lot but not able to do it. Please help.
My code both Markup and C# is given below
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<script type = "text/javascript">
function check_click(objRef) {
    //Get the Row based on checkbox
    var col = objRef.parentNode;
    if (objRef.checked) {
        check_click.Enabled = true;
    }
}
</script>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label5" runat="server" Text="Label">
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
         Height="250px" style="font-family: Verdana; font-size: small" 
        Width="677px" onselectedindexchanged="GridView1_SelectedIndexChanged" 
        GridLines="None">
        <alternatingrowstyle backcolor="#E8E8E8" />
        <columns>
            <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
            <asp:BoundField DataField="Pages" HeaderText="Pages" />
           <asp:TemplateField HeaderText="Add" SortExpression="Add">
                <itemtemplate>
                    <asp:CheckBox ID="Add" runat="server"  onclick="Javascript:check_click(this)"/>
                </itemtemplate>
            
            <asp:TemplateField HeaderText="View" SortExpression="View">
                <itemtemplate>
                    <asp:CheckBox ID="View" runat="server"   />
                </itemtemplate>
            
            <asp:TemplateField HeaderText="Edit" SortExpression="Edit">
                <itemtemplate>
                    <asp:CheckBox ID="Edit" runat="server" />
                </itemtemplate>
            
            <asp:TemplateField HeaderText="Delete" SortExpression="Delete">
                <itemtemplate>
                    <asp:CheckBox ID="Delete" runat="server" />
                </itemtemplate>
            
        </columns>
        <HeaderStyle BackColor="#EBEBEB" />
    
    <asp:Button ID="Button7" runat="server" onclick="Button7_Click" Text="Save" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" 
        ErrorMessage="CustomValidator" ClientValidationFunction="check_click">Required
    <br />

protected void Button7_Click(object sender, EventArgs e)
  {
      foreach (GridViewRow row in GridView1.Rows)
      {
          if (row.RowType == DataControlRowType.DataRow)
          {
              if (Session["Role_Name"] != null && !string.IsNullOrEmpty(Session["Role_Name"].ToString()))
              {
                  CheckBox chkAdd = (row.Cells[2].FindControl("Add") as CheckBox);

                  CheckBox chkEdit = (row.Cells[3].FindControl("View") as CheckBox);

                  CheckBox chkView = (row.Cells[4].FindControl("Edit") as CheckBox);

                  CheckBox chkDelete = (row.Cells[5].FindControl("Delete") as CheckBox);

                  // string strID = row.Cells[0].Text;
                  string ID = row.Cells[0].Text;
                  string Pages = row.Cells[1].Text;
                  con.Open();

                  string query = "Update Role1 set [Add]='" + (chkAdd.Checked == true ? 'Y' : 'N') + "', [View]='" + (chkView.Checked == true ? 'Y' : 'N') + "' ,[Edit]='" + (chkEdit.Checked == true ? 'Y' : 'N') + "' ,[Delete]='" + (chkDelete.Checked == true ? 'Y' : 'N') + "' where Pages ='" + Pages + "' and Role_Name='" + Session["Role_Name"].ToString() + "'and ID='" + ID + "'";
                  SqlCommand cmd = new SqlCommand(query, con);
                  cmd.ExecuteNonQuery();

                  con.Close();

              }
          }
      }
      populateGridview();

      GridView1.DataBind();
  }
Posted
Updated 15-Apr-14 23:11pm
v2
Comments
jacobjohn196 16-Apr-14 4:31am    
What You have written in GridView1_SelectedIndexChanged event?
Member 10578683 16-Apr-14 5:02am    
nothing
Member 10578683 16-Apr-14 5:06am    
i have not written anything in GridView1_SelcectedIndexChanged event

1 solution

You could try this,
<asp:CheckBox ID="View" runat="server" EnableViewState="true"   />
 
Share this answer
 
v2
Comments
Member 10578683 16-Apr-14 6:01am    
i tried like this still it is not coming
Member 10578683 16-Apr-14 7:08am    
i wrote like inside button click
if(!Ispoastback)
{
populategridview();
}
then it is coming but when i am closing this page and again opens it disappears

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