简体   繁体   English

从gridview C#检索多个检查值

[英]retrieve multiple checked values from gridview C#

I want to retrieve multiple checked values(id's) from asp:checkbox in my gridview. 我想从我的gridview中的asp:checkbox检索多个检查值(id)。 With the selected records, i want to do a delete function with nhibernate. 对于选定的记录,我想使用nhibernate进行删除。 The problem is that im getting only one value(id) from the gridview, even if i select multiple records. 问题是,即使我选择了多个记录,我也只能从gridview中获取一个值(id)。 My goal is to delete the checked items in the gridview. 我的目标是删除gridview中的选中项。 My code: 我的代码:

<ItemTemplate>
   <asp:Label ID="labelID" runat="server" Text='<%# Eval("Accountant") %>' />
</ItemTemplate>

<ItemTemplate>
   <asp:CheckBox ID="cbDelete" runat="server" AutoPostBack="true"
                    oncheckedchanged="cbDelete_CheckedChanged" /> 
</ItemTemplate>

This one works. 这个作品。 The list gets all the values from the checked items. 该列表从选中的项目中获取所有值。

protected void btnDelete_Click(object sender, EventArgs e)
{
      List<int> lstE = new List<int>();
      foreach (GridViewRow gridViewRow in gvAccountants.Rows)
      {
            if (((CheckBox)gridViewRow.FindControl("cbDelete")).Checked == true)
            {
                string ID = ((Label)gridViewRow.FindControl("labelID")).ToString();
                int n = Convert.ToInt32(ID);
                lstE.Add(n);
            }
      }

      this.Accountant = Data.Instance.NHibernateSession.Load<Accountant>(lstE);
      Data.Instance.NHibernateSession.Delete(this.Accountant);
      Data.Instance.NHibernateSession.Flush();
}

The problem is resolved with a foreach loop for my list items: 我的列表项的foreach循环解决了该问题:

foreach (int nn in lstE)
{
  //delete function
}

On Button click just put this code and remove AutoPostBack="true" oncheckedchanged="cbDelete_CheckedChanged" From your gridview checkbox 在“按钮”上单击,只需放入此代码,然后从gridview复选框中删除AutoPostBack="true" oncheckedchanged="cbDelete_CheckedChanged"

//remove checked rows
        protected void btn_removeall_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow gr in grid.Rows)
                {
                    CheckBox cc = (CheckBox)gr.FindControl("cbDelete");
                    if (cc.Checked == true)
                    {                  
                        string id = grid.DataKeys[gr.RowIndex].Values["ID"].ToString();        
                        //
                        //call your delete function here
                        //
                    }
                }           
            }
            catch (Exception ex) { }
        }

it sounds like you are loading your gridview from within Page_Load, which is fine. 听起来您是从Page_Load中加载gridview的,这很好。 but in your case you would need to make sure your gridview loading code looks like this: 但是在您的情况下,您需要确保您的gridview加载代码如下所示:

if(!Page.IsPostBack)
{
  //gridview loading code
}

this prevents your gridview from being reloaded (and losing which boxes are checked) when you click your delete button 当您单击删除按钮时,这可以防止重新加载gridview(并丢失选中的框)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM