Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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();
               
            }
        }
In .aspx page
<columns>
     <templatefield>
        <itemtemplate>
            <asp:CheckBox ID="ChkBoxATContact&quot" runat="server" /> 
        </itemtemplate>
      
</templatefield></columns>
                           
Although I select rows, but i get checked = false...
Posted 18-Feb-13 1:29am
mona_12378
Edited 18-Feb-13 1:55am
RyanDev92.2K
v2
Comments
Asim Mahmood at 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.
ryanb31 at 18-Feb-13 7:55am
   
Where's the code that checks the boxes?
Sandeep Mewara at 18-Feb-13 8:09am
   
1. What error?
2. Where is code for deleting?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
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; }
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 459
1 OriginalGriff 441
2 sanket saxena 349
3 Tadit Dash 310
4 Abhinav S 235
0 Sergey Alexandrovich Kryukov 12,109
1 OriginalGriff 7,326
2 Peter Leow 5,013
3 Abhinav S 4,013
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid