在GridView中删除多个复选框 [英] delete multiple checkbox in gridview

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

问题描述

我遇到一个问题.当我选择标头中的复选框而不是itemtemples中的其他复选框时,
未选择.我要删除多个记录.代码如下.

i have one problem.when i select checkbox in header than other checkbox in itemtemples
not selected.i want to delete multiple records. code is given below.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="hdrcbx" runat="server" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="cbx" runat="server" />
                    </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
                    <asp:BoundField DataField="Name" HeaderText="Name" />
                    <asp:BoundField DataField="Address" HeaderText="Address" />

            </Columns>
        </asp:GridView>





public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\rAvIrAj\Documents\Visual Studio 2010\WebSites\WebSite1\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
    //SqlCommand cmd;
    DataSet ds;
    SqlDataAdapter adp;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        ds = new DataSet();
        adp = new SqlDataAdapter("select * from student",con);
        adp.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        CheckBox check = (CheckBox)GridView1.FindControl("hdrcbx");
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox inner = (CheckBox)gr.FindControl("cbx");
            if (check.Checked)
            {
                inner.Checked = true;
            }
        }
       
        
    }
    
     

}



请给我答案.



please give me answer.

thanx in advance.

推荐答案

.当我选择标题中的复选框而不是未选中itemtemples中的其他复选框时.我要删除多个记录
看看这些知识库文章:
批量删除(C#) [ ^ ]
删除多行记录Gridview复选框确认 [将复选框添加到ASP.NET GridView控件 [ ^ ]

为了检查所有功能,您需要将JS代码附加到Grid的RowDataBound事件中的复选框,在该事件中,将调用rowbind.完成此操作后,单击即可工作.

看看这些:
将JavaScript与ASP.Net GridView控件一起使用 [ ^ ]
使用不带RowDataBound代码的jQuery [ ^ ]
.when i select checkbox in header than other checkbox in itemtemples not selected.i want to delete multiple records
Have a look at these knowledge-base articles:
Batch Deleting (C#)[^]
Delete Multiple Rows Records Gridview CheckBox Confirmation[^]
Add Checkboxes to ASP.NET GridView Control[^]

For check all feature, you need to attach the JS code to checkbox in Grid''s RowDataBound event where very rowbind is called. Once this is done, single click would work.

Have a look at these:
Using JavaScript with ASP.Net GridView Control[^]
Using jQuery without RowDataBound code[^]


将此内容写入您的aspx文件中.

write this in your aspx file.

<script type="text/javascript">
    function SelectAll(id) {
        var frm = document.forms[0];
        for (i=0;i<frm.elements.length;i++) {

            if (frm.elements[i].type == "checkbox") {

                frm.elements[i].checked = document.getElementById(id).checked;

            }

        }

    }

</script>



并在您的C#代码中



and in your C# code

protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        //adding an attribute for onclick event on the check box in the header
        //and passing the ClientID of the Select All checkbox
        ((CheckBox)e.Row.FindControl("hdrcbx")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("hdrcbx")).ClientID + "')");
    }
}


<asp:TemplateField>
                  <HeaderTemplate>
                      <asp:CheckBox ID="hdrcbx" runat="server" />
                  </HeaderTemplate>
                  <ItemTemplate>
                      <asp:CheckBox ID="cbx" runat="server" />
                  </ItemTemplate>




您可以改用以下代码:-




you can used instead this code:-

<asp:TemplateField>
                                                <HeaderTemplate>
                                                    <asp:CheckBox ID="chkSelectAll" runat="server" Text="Select All" AutoPostBack="True"

                                                        OnCheckedChanged="chkSelectAll_CheckedChanged" />
                                                </HeaderTemplate>
                                                <ItemTemplate>
                                                    <asp:CheckBox runat="server" ID="chkSelect" /></ItemTemplate>
                                            </asp:TemplateField>


和二手按钮


and used button

<asp:Button ID="btnImport" runat="server" Text="Import" OnClick="btnImport_Click" Width="100px"/>


和后面的代码使用了:-








and code behind used this:-







protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
     {
         CheckBox chkSelectAll = sender as CheckBox;

         chkSelectAll.Text = chkSelectAll.Checked ? "Deselect All" : "Select All";

         foreach (GridViewRow gvr in showDetails.Rows)
         {
             CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;

             if (chkSelect != null)
             {
                 chkSelect.Checked = chkSelectAll.Checked;
             }
         }
     }




对于多次删除,您可以使用它:-





and for multiple deletion you can used it:-


protected void btnImport_Click(object sender, EventArgs e)
      {
          int counter = 0;
          if (showDetails.Rows.Count == 0)
          {

              divMsg.InnerHtml = "Please first choose file and Click show button Than Click Import Button...";
              return;
          }
          foreach (GridViewRow gvr in showDetails.Rows)
          {
              CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;

              if (chkSelect != null && chkSelect.Checked == true)
              {
                  counter++;

              }

          }
          if (counter == 0)
          {
              divMsg.InnerHtml="Please Select at least One Check Box.";
              return;
          }
          else
          { write your deletion data base  code}


}}



i hope this will help you and not please give me your comment


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

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