gridview中的复选框有问题 [英] Problem with checkbox inside gridview

查看:59
本文介绍了gridview中的复选框有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个包含复选框的模板字段的gridview。我有一个按钮outode gridview,它被称为保存按钮。当我检查检查并单击保存按钮然后y存储在数据库中,否则N.my问题是当我点击保存按钮时,检查的cheboes变为未选中状态。我希望看到已选中的复选框,直到我取消选中它们。我尝试了很多但却无法做到。请帮忙。

我的代码Markup和C#如下所示

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();
  }

推荐答案

你可以试试这个,

You could try this,
<asp:CheckBox ID="View" runat="server" EnableViewState="true"   />


这篇关于gridview中的复选框有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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