Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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();
               
            }
        }

In .aspx page
XML
<columns>
     <templatefield>
        <itemtemplate>
            <asp:CheckBox ID="ChkBoxATContact"" runat="server" /> 
        </itemtemplate>
      
</templatefield></columns>

Although I select rows, but i get checked = false...
Posted
Updated 18-Feb-13 1:55am
v2
Comments
Asim Mahmood 18-Feb-13 7:54am    
If you checkbox is unchecked then you must check your code may be while clicking Delete button your grid is binding again.
ZurdoDev 18-Feb-13 7:55am    
Where's the code that checks the boxes?
Sandeep Mewara 18-Feb-13 8:09am    
1. What error?
2. Where is code for deleting?

1 solution

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.NET
<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>

code bebind as follows
C#
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; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900