【发布时间】:2021-07-04 08:09:35
【问题描述】:
我创建了一个带有 GridView 的 asp 页面,其中包含一个文本列和 n 个带有动态复选框的列。
<asp:GridView ID="gridAssetto" runat="server" Width="100%" GridLines="Vertical" SkinID="grdRegular"
EnableModelValidation="True" AllowPaging="false"
OnRowCommand="gridAssetto_RowCommand" OnRowDataBound="gridAssetto_RowDataBound"
OnDataBound="gridAssetto_DataBound">
</asp:GridView>
在.cs中
protected void gridAssetto_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i < numCol; i++)
{
CheckBox cb = new CheckBox();
cb.ID = "cb_Aero" + e.Row.Cells[0].Text + "_Ane" +
gridAssetto.HeaderRow.Cells[i].Text;
cb.EnableViewState = true;
MutuallyExclusiveCheckBoxExtender cbExt = new MutuallyExclusiveCheckBoxExtender();
cbExt.ID = "cbExt" + i;
cbExt.TargetControlID = cb.ID;
cbExt.EnableViewState = true;
cbExt.Key = e.Row.RowIndex.ToString();
e.Row.Cells[i].Controls.Add(cb);
e.Row.Cells[i].Controls.Add(cbExt);
}
我从数据库中获取数据并使用方法正确设置复选框
foreach (GridViewRow row in gridAssetto.Rows)
{
CheckBox chk = (CheckBox)row.FindControl(cbID);
if (chk != null)
chk.Checked = true;
}
当我想将信息存储在数据库中时,我必须读取用户修改的复选框,单击按钮时 FindControl 方法不起作用,好像网格中的复选框不再存在
foreach (GridViewRow row in gridAssetto.Rows)
{
for (int xx = 1; xx < row.Cells.Count; xx++)
{
if (row.RowType == DataControlRowType.DataRow)
{
string idcell = row.Cells[xx].ID;
CheckBox chk = (CheckBox)row.Cells[xx].FindControl(cbID);
if (chk != null)
{
if (chk.Checked)
{
my code ...
}
}
}
}
}
chk 始终为空! 在页面 html 中,在 clic 按钮之前,我有这个控件:
<input id="ctl00_Contenitore_TabGestione_TabAssetto_gridAssetto_ctl05_cb_AeroG001769_Ane1128" type="checkbox" name="ctl00$Contenitore$TabGestione$TabAssetto$gridAssetto$ctl05$cb_AeroG001769_Ane1128">
但是当我阅读调试代码中的检查时,我有值
row.Cells[xx].ID is null and
row.Cells[xx].UniqueID is
"ctl00$Contenitore$TabGestione$TabAssetto$gridAssetto$ctl03$ctl00"
谢谢
【问题讨论】:
标签: c# html asp.net checkbox gridview