How to programmatically create and use a list of checkboxes from ASP.NET?

前端 未结 7 1242
礼貌的吻别
礼貌的吻别 2021-01-19 10:37

I have a page with a table of stuff and I need to allow the user to select rows to process. I\'ve figured out how to add a column of check boxes to the table but I can\'t se

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 10:42

    First, make sure that each Checkbox has an ID and that it's got the 'runat="server"' in the tag.

    then use the FindControl() function to find it.

    For example, if you're looping through all rows in a GridView..

    foreach(GridViewRow r in Gridview1.Rows)
    {
    
        object cb = r.FindControl("MyCheckBoxId");
        if(r != null)
        {
          CheckBox chk = (CheckBox)cb;
          bool IsChecked = chk.Checked;
        }
    
    }
    

提交回复
热议问题