Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i am trying to implement a simple example of bubbling in Gridview WITH two checkboxes(headertemplate("chkall") and itemtemplate("chksingle")) in Gridview. I m using c# 4.0 with sql server 2008.

I have two issues in this simple program:
a) whenever i am trying to check/uncheck the chkall(whose autopostback proerty is set to 'true'), it does nt work.

b) WHen i chk on some checkboxes, and click on a 'delete' button somewhere on the web-form, it doesnot work...
please help me rectifying the following code....

protected void chkall_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
        CheckBox chksingle = (CheckBox)row.Cells[0].FindControl("chksingle");
         chksingle.Checked =  ((CheckBox)sender).Checked;
         }
      }


      void DBind()
        {
     con = new SqlConnection("server=.;database=shikhar;integrated security=true");
     adap = new SqlDataAdapter("select * from emp", con);
     ds = new DataSet();
     adap.Fill(ds, "emp");
     GridView2.DataSource = ds.Tables[0];
     GridView2.DataBind();
     con.Close();
      }


        protected void Button2_Click(object sender, EventArgs e)
        {
 SqlConnection con = new SqlConnection("server=.;database=shikhar;integrated security=true");
      con.Open();
      foreach (GridViewRow row in GridView2.Rows)
            {
   CheckBox chksingle = row.Cells[0].FindControl("chksingle") as CheckBox;
             if (chksingle.Checked)
                {
  SqlCommand cmd = new SqlCommand("delete employee where id='"+row.Cells[1].Text+"'",con);
              
                    cmd.ExecuteNonQuery();
                }
            }
            con.Close();
            con.Dispose();
            DBind();
        }
    }


[Edited]Code is wrapped in "pre" tags[/Edited]
[edit]Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response. - OriginalGriff[/edit]
Posted
Updated 2-Aug-11 2:39am
v4

HI shikhar before suggest you some changes i need some information from your end Those are as follows.

1]Is your gridview control is in UpdatePanel.if yes then check that have you set the AsyncPostBackTrigger for gridcontrol or not.if no then please set it first then it should work.

2]Did you set the onCheckedchange event for the checkbox control or not because we won't be able to set it directly by double clicking it.we need to explicitly copy that event name and past it in source code.for the control.

3]if given probability are already in place then try to run your code after commenting the event of checkbox and see is there any exception are thrown or not.

4] if you assign an event to the control then check how you assign it.It should like the given below example.OncheckedChanged="chkAll_CheckedChanged"

Try those checks might be those will be helpffull for you.Enjoy programming.
 
Share this answer
 
Comments
shikhar gilhotra 3-Aug-11 8:33am    
HI,
1) gridview is not in updatepanel
2)yes i did.
3)yet,not working..
4)didnt work..
I think the problem is in the Page_Load event and You are binding the grid everytime whenevr the page is refreshed. So put the binding login in the !isPostBack and check
 
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