开发者

Asp.net CheckBox within GridView looses its value

I have a GridView which has bound fields and a template field for checkbox. I wrote a code for deletion of records as per checking checkboxes. My problem is

HtmlInputCheckBox chk;
foreach(GridViewRow dr in dgvdetails.Rows)
{
    chk = (HtmlInputCheckBox)dr.FindControl("ch");
    chk.Checked = true;
    if (chk.Checked)///       **here checkbox is not checked even if I'm check it**
    {
        pl.id = int开发者_如何转开发.Parse(chk.Value);
        bl.deletedgvdetails(pl);
    }
}


There are two things that come to mind that could be at play here. I'm not sure when your code is running - with regard to the page lifecycle - but if it is being handled in a button click event you may want to make sure that your grid view has not been "rebound" during a page event such as Page_Load which runs prior to button click events firing. IF this is the case, all of your check boxes will have been reinitialized before your delete method runs.

Secondly, inside your foreach loop you may want to pay attention to the RowType of the current row. The first iteration through the loop may be focusing on the Header Row (if you have it enabled for the GridView) and therefore may not have the checkbox.

Checking that if(chk != null) {} is always a good idea if you use e.Row.FindControl() - just in case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜