Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How the checkboxes remains checked after i click on the save button.
C#
protected void Button3_Click(object sender, EventArgs e)
{

    //for (int i = 0; i < GridView1.Rows.Count; i++)
    //{
    //    CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[2].FindControl("CheckBox1");
    //    if (chk != null)
    //    {
    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
            if (chkRow.Checked)
            {

                // if (!Page.IsPostBack)

                string strID = row.Cells[0].Text;
                string Pages = row.Cells[1].Text;
                string status = "Y";


                con.Open();
                string query = "Update Role1  set [Add] ='" + status + "' where ID= '" + strID + "'  and Pages= '" + Pages + "'";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();

                GridView1.DataBind();
                con.Close();
            }


            else
            {

                string strID = row.Cells[0].Text;
                string Pages = row.Cells[1].Text;
                string status = "N";



                //cmd = new SqlCommand("insert into Role1(Checkin_Add) values(" + status + " where ID= '" + strID + "' and Role_Name='" + strName + "')", con);
                string query = "Update Role1  set [Add] ='" + status + "' where ID= '" + strID + "'  and Pages= '" + Pages + "'";
                SqlCommand cmd = new SqlCommand(query, con);
                con.Open();
                cmd.ExecuteNonQuery();

                GridView1.DataBind();
                con.Close();
            }
        }
    }

Button 3 is the save button.
Posted
v2
Comments
What happens now? Do CheckBoxes get unchecked on Button Click or remain checked?
Member 10578683 24-Mar-14 9:07am    
i want it remain checked when i m clicking the save button
Member 10578683 24-Mar-14 9:14am    
but when i am clicking the save button it gets unchecked

try using chkRow.Checked=false u need to uncheck them if u dont want to see check mark on save button
 
Share this answer
 
Comments
Member 10578683 24-Mar-14 8:06am    
whatever i checked after clicking the check button i want to c it checked. but it is automatically unchecked
Hi,
Put your page_load code inside if(!isPostBack).

C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Your existing code
            }
        }


Hope this will solve your problem.
 
Share this answer
 
Comments
Member 10578683 24-Mar-14 9:22am    
protected void Page_Load(object sender, EventArgs e)
{

try
{

if (!Page.IsPostBack)
{
BindGrid();
}
}
catch (Exception ex)
{
}

//



}

protected void Button2_Click(object sender, EventArgs e)
{


DropDownList1.Items.Remove(DropDownList1.SelectedItem);
con.Open();
string query = "Delete from Role2 Where Role_Name <> 'Administrator' ";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();

}




protected void Button1_Click1(object sender, EventArgs e)
{
string fruit = TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(fruit))
{
DropDownList1.Items.Add(new ListItem(fruit, fruit));

}


string role = TextBox1.Text;
con.Open();
string str = "insert into [Role2] (Role_Name) values(@Role_Name)";
using (SqlCommand insertuser = new SqlCommand(str, con))
try
{
insertuser.Parameters.AddWithValue("@Role_Name", TextBox1.Text);

insertuser.ExecuteNonQuery();

}
catch (Exception ex)
{
}
con.Close();


}
private void BindGrid()
{
string qry = "select distinct * from Role1";
DataSet ds = dut.GetDataSet(qry);
GridView1.DataSource = ds;
GridView1.DataBind();

}


protected void Button3_Click(object sender, EventArgs e)
{

//for (int i = 0; i < GridView1.Rows.Count; i++)
//{
// CheckBox chk = (CheckBox)GridView1.Rows[i].Cells[2].FindControl("CheckBox1");
// if (chk != null)
// {
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
if (chkRow.Checked)
{



string strID = row.Cells[0].Text;
string Pages = row.Cells[1].Text;
string status = "Y";


con.Open();
string query = "Update Role1 set [Add] ='" + status + "' where ID= '" + strID + "' and Pages= '" + Pages + "'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();

BindGrid();
con.Close();
}


else
{

string strID = row.Cells[0].Text;
string Pages = row.Cells[1].Text;
string status = "N";



//cmd = new SqlCommand("insert into Role1(Checkin_Add) values(" + status + " where ID= '" + strID + "' and Role_Name='" + strName + "')", con);
string query = "Update Role1 set [Add] ='" + status + "' where ID= '" + strID + "' and Pages= '" + Pages + "'";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
cmd.ExecuteNonQuery();

BindGrid();
con.Close();
}

}

}

}

}
This is my whole code please check.
Look, when you click Save button, it posts back. So, Page again loads with the initial stage.

So, you need to store the checked status somewhere either in Session and ViewState. Then retrieve it inside Button Click and check them again according to their checked status.

Check the article - Maintaining State of CheckBoxes While Paging in a GridView Control[^] to learn the concept.
 
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