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
RyanDev108.4K
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 483
1 OriginalGriff 333
2 Maciej Los 305
3 CHill60 220
4 manoj kumar choubey 184
0 OriginalGriff 6,367
1 Sergey Alexandrovich Kryukov 5,296
2 Maciej Los 3,384
3 Peter Leow 3,259
4 DamithSL 2,490


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100