在gridview中选择要删除的多行时出错? [英] Getting error on selecting multiple rows for deleting in gridview..?

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

问题描述

我想使用gridview中的复选框选择多行。我想删除这些行。我写了删除代码。但是Checked值没有得到,因此它会产生错误...



我的代码是 -

In .cs page

I want to select multiple rows using checkbox in gridview.. And i want to delete those rows. I wrote the code for deleting. But Checked value is not getting, hence it generates the error...

my code is-
In .cs page

foreach (GridViewRow row in GrdATContactList.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("ChkBoxATContact") as CheckBox;
            if (((CheckBox)row.FindControl("ChkBoxATContact")).Checked)
            {
                string id = GrdATContactList.DataKeys[row.RowIndex].Value.ToString();
               
            }
        }



在.aspx页面


In .aspx page

<columns>
     <templatefield>
        <itemtemplate>
            <asp:CheckBox ID="ChkBoxATContact"" runat="server" /> 
        </itemtemplate>
      
</templatefield></columns>



虽然我选择了行,但是我得到了检查= false ...


Although I select rows, but i get checked = false...

推荐答案

当你绑定网格时你应该有条件地绑定它。条件是if(!page.IsPostBack)然后绑定网格。在您将表单发回服务器的情况下,它将重新绑定,并且您的复选框选择将被覆盖。就像这样的代码示例



When you bind the grid you should bind it conditionally. Condition would be if (!page.IsPostBack) then you bind grid. In your case when form post back to the server it is rebind and your checkbox selection is override. Just code example like that

<asp:gridview id="GrdATContactList" runat="server" autogeneratecolumns="false" datakeynames="Id" xmlns:asp="#unknown">
    <columns>
        <asp:templatefield headertext="Is Delete">
            <itemtemplate>
                <asp:checkbox id="chkDelete" runat="server" />
            </itemtemplate>
            <edititemtemplate>
                <asp:textbox id="TextBox1" runat="server" text="Hello"></asp:textbox>
            </edititemtemplate>
        </asp:templatefield>
        <asp:boundfield datafield="Id" />
        <asp:boundfield datafield="Name" />
    </columns>
</asp:gridview>



代码bebind如下


code bebind as follows

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GrdATContactList.DataSource = GetData();
            GrdATContactList.DataBind();
        }
    }
    private IList<Data> GetData()
    {
        var list = new List<Data>();
        list.Add(new Data {Id=1, Name="A1"});
        list.Add(new Data { Id = 2, Name = "A2" });
        list.Add(new Data { Id = 3, Name = "A3" });
        return list;
    }
    protected void OnDeleteAll(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GrdATContactList.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkDelete") as CheckBox;
            if (((CheckBox)row.FindControl("chkDelete")).Checked)
            {
                string id = GrdATContactList.DataKeys[row.RowIndex].Value.ToString();

            }
        }
    }
}
public class Data
{
    public int Id { get; set; }
    public string Name { get; set; }
}


这篇关于在gridview中选择要删除的多行时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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