单击“保存”按钮后如何检查复选框 [英] How The Checkboxes Remains Checked After I Click On The Save Button

查看:95
本文介绍了单击“保存”按钮后如何检查复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击保存按钮后,如何保持选中复选框。

 受保护  void  Button3_Click( object  sender,EventArgs e)
{

// for(int i = 0; i< GridView1.Rows.Count; i ++)
< span class =code-comment> // {
// <跨度类= 代码注释>复选框CHK =(复选框)GridView1.Rows [I] .Cells [2] .FindControl( CheckBox1);
<跨度类=代码 - 注释> // if(chk!= null)
// {
foreach (GridViewRow row in GridView1.Rows)
{
if if (row.RowType == DataControlRowType.DataRow)
{
复选框chkRow =(row.Cells [<跨度类= 代码位> 2 ]。的FindControl( CheckBox1 as CheckBox);
if (chkRow.Checked)
{

// if(!Page.IsPostBack)

string strID = row .Cells [ 0 ]。文字;
string Pages = row.Cells [ 1 ]。文字;
string status = Y;


con.Open();
string query = 更新Role1 set [添加] =' + status + '其中ID =' + strID + '和Pages =' + Pages + ';
SqlCommand cmd = new SqlCommand(query,con);
cmd.ExecuteNonQuery();

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


其他
{

string strID = row.Cells [ 0 ]。文字;
string Pages = row.Cells [ 1 ]。文字;
string status = N;



// cmd = new SqlCommand(insert into基于role1(Checkin_Add)值( +状态+ 其中ID = ' + strID + '和ROLE_NAME =' +则strName + '),CON);
<跨度类= code-keyword> string
query = 更新Role1 set [Add] =' + status + '其中ID =' + strID + '和Pages =' + Pages + ';
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
cmd.ExecuteNonQuery();

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



按钮3是保存按钮。

解决方案

尝试使用chkRow.Checked = false如果你不想在保存按钮上看到复选标记,你需要取消选中它们


你好,

把你的page_load code if if(!isPostBack)。



  protected   void  Page_Load( object  sender,EventArgs e)
{
if (!Page.IsPostBack)
{
// 您现有的代码
}
}





希望这可以解决您的问题。


看,当你点击保存按钮时,它会回发。因此,Page会在初始阶段再次加载。



因此,您需要将检查状态存储在 Session ViewState 。然后在按钮单击中检索它,并根据其检查状态再次检查它们。



检查文章 - Maintaining状态的[ ^ ]学习这个概念。


How the checkboxes remains checked after i click on the save button.

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.

解决方案

try using chkRow.Checked=false u need to uncheck them if u dont want to see check mark on save button


Hi,
Put your page_load code inside if(!isPostBack).

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



Hope this will solve your problem.


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.


这篇关于单击“保存”按钮后如何检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆