我在asp.net中有以下网格
foreach (GridViewRow dgvr in grdDWlocations.Rows)
{
(CheckBox)dgvr.FindControl("chk_DWlocReq")=true;
}
我想在
后面的代码中为“chk_DWlocReq”赋值 像这样.sublime-package
但高于一个无效,这怎么可以正常?
答案 0 :(得分:3)
我相信您的代码必须更改为:
foreach (GridViewRow dgvr in grdDWlocations.Rows)
{
((CheckBox)dgvr.FindControl("chk_DWlocReq")).Checked=true;
}
答案 1 :(得分:2)
使用此:
foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
CheckBox myCheckBoxID = row.FindControl("myCheckBoxID") as CheckBox;
}
myCheckBoxID.Checked = true;
}
OR 如果您正在处理RowDataBound事件,它就像这样:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox myCheckBoxID = e.Row.FindControl("myCheckBoxID") as CheckBox;
}
myCheckBoxID.Checked = true;
}
答案 2 :(得分:0)
尝试以下方法:
CheckBox checkbox1 = dgvr.FindControl("chk_DWlocReq") as CheckBox;
checkbox1.Checked = true;
答案 3 :(得分:0)
我使用了以下代码:
ds1.SelectCommand = String.Format("exec myStoredproc");
dv1 = (DataView)ds1.Select(DataSourceSelectArguments.Empty);
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (dv1.Table.Rows[i]["ReceiveNotification"].ToString() == "1")
{
((CheckBox)GridView1.Rows[i].FindControl("GV_chkNotification")).Checked = true;
}
else
{
((CheckBox)GridView1.Rows[i].FindControl("GV_chkNotification")).Checked = false;
}
}