Click here to Skip to main content
14,735,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
this code is working other system visual studio but my system is not working

protected void Button1_Click1(object sender, EventArgs e)
    foreach (GridViewRow gdv in GridView1.Rows)
        CheckBox chk = (gdv.Cells[0].FindControl("chkbox1") as CheckBox);

        if (chk.Checked == true)
            var Label = gdv.Cells[0].FindControl("lblid") as Label;
            SqlConnection cn = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("Delete from tbl_login where Id=@Id", cn);
            cmd.Parameters.AddWithValue("@Id", int.Parse(Label.Text));
            int id = cmd.ExecuteNonQuery();



this code is my system is not working
checkBox checked is always so 'False' why
checkBok Checked is not show 'true'
if another idea about delete row on checkBox Check so plz help me. only button event

What I have tried:

plz help me sir
why show always false checkbox checked
Updated 28-Apr-17 22:36pm
Andy Lanng 26-Apr-17 11:24am
not true. it reads from the first cell on each row. read the code again (I too hate ints in code and always have the knee jerk reaction that that is where the flaw is).
BTW: how many "that"'s can you fit into a sentence in a row (hint: the record is 15)
Andy Lanng 26-Apr-17 11:37am
I wrote the word "that" on a board
I guy called That wrote the word "that" on a board:
"I saw that 'that' that That wrote".
Now the word has lost all meaning to me
Bryian Tan 26-Apr-17 11:48am
I misread :(
Andy Lanng 26-Apr-17 11:53am
Misread is understandable. Misunderstood is an issue. Admitting either is admirable. further understanding is learn-able and achievable. Buy Coke
Oh s*&t got into slogans again :P
Andy Lanng 26-Apr-17 11:21am
ur my guurd. I hate seeing static ints, but here it's fine.
The issue must be in when the function is called.

Button1 click does not update the form. it acts on it. I feel that the issue is that buton1 does not submit the form so (asp?) server side does not see the change. Can you confirm this?
Andy Lanng 26-Apr-17 11:42am
PS: you can "discover" the form state on a button click, but with (asp?) it's easier to submit. This is why, as an ASP developer myself, I never use ASP.Net!
F-ES Sitecore 26-Apr-17 11:38am
The problem might be that you're binding the gridview to its data on every postback, not just when the page loads. So in your Page_Load you'll have

GridView1.DataSource = myData;

That means when you click something that does a postback the gridview is refreshed from new so the fact that you had items checked is forgotten\overridden. So only bind the data when the page is not a postback and it will keep its state when you click buttons etc

if (!Page.IsPostBack)
GridView1.DataSource = myData;
ZurdoDev 26-Apr-17 11:40am
That's probably it.
Andy Lanng 26-Apr-17 11:44am
Ooh. that might be it. But then the OP would see a form change. The OP didn't mention that, but that is where I fail and House finds fault (2M points if you get the reference)
Member 10506503 29-Apr-17 3:59am
thanks sir
Andy Lanng 26-Apr-17 11:46am
PS: I think the OP is no longer here
Maciej Los 26-Apr-17 16:04pm
Are you sure that checkbox's name is: "chkbox1"?

There are lots of answer available on google.
Here is some references

Link 1

Link 2

Link 3

Better to spend some time to understand code logic.
Member 10506503 29-Apr-17 4:05am
ok sir

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