Asp.net 如何在gridview OnRowEditing中获取复选框
如果有人可以帮助我,我在gridview中遇到问题,当我使用OnRowEditing和onRowDelete时,在asp:TemplateField、ItemTemplate中,我使用的是复选框,当选中复选框中的复选框时。checked值从不为true, 下面是我的C#脚本Asp.net 如何在gridview OnRowEditing中获取复选框,asp.net,Asp.net,如果有人可以帮助我,我在gridview中遇到问题,当我使用OnRowEditing和onRowDelete时,在asp:TemplateField、ItemTemplate中,我使用的是复选框,当选中复选框中的复选框时。checked值从不为true, 下面是我的C#脚本 C# 受保护的无效按钮1\u单击(对象发送者,事件参数e) { foreach(GV_程序中的GridViewRow行。行) { 如果(((复选框)row.FindControl(“cbSelect”)。选中) { //删
C#
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
foreach(GV_程序中的GridViewRow行。行)
{
如果(((复选框)row.FindControl(“cbSelect”)。选中)
{
//删除某些内容(永远不要到这里)
}
}
}
一步一个脚印,直到你能让它工作
试试这个:
/* put this value at the "class level" */
public static readonly int GRID_VIEW_COLUMN_ORDINAL_chkBoxSELECT = 3; /* Your number may be different, its the ordinal column number. A static readonly or const value makes your code more readable IMHO */
/*Then in your method */
if (null != this.GV_program)
{
Control cntl = null;
foreach (GridViewRow gvr in this.GV_program.Rows)
{
cntl = gvr.Cells[GRID_VIEW_COLUMN_ORDINAL_chkBoxSELECT].FindControl("cbSelect");
CheckBox cbIsApproved = cntl as CheckBox;
if (null != cbIsApproved)
{
bool myValue = cbIsApproved.Checked;
}
}
}
我刚刚运行了这个代码,它对我有效
protected void imgbutSave_Click(object sender, ImageClickEventArgs e)
{
if (null != this.gvMain)
{
Control cntl = null;
string finalMsg = string.Empty;
int counter = 0;
StringBuilder sb = new StringBuilder();
foreach (GridViewRow gvr in this.gvMain.Rows)
{
counter++;
cntl = gvr.Cells[GRID_VIEW_COLUMN_ORDINAL_chkBoxIsApproved].FindControl("chkBoxIsApproved");
CheckBox cbIsApproved = cntl as CheckBox;
if (null != cbIsApproved)
{
sb.Append(string.Format("Row '{0}' (chkBoxIsApproved.Checked) = '{1}'", counter, cbIsApproved.Checked) + System.Environment.NewLine);
}
}
finalMsg = sb.ToString();
}
}
还有我的aspx代码
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkBoxIsApproved" runat="server" Checked='<%#Eval("IsApproved")%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
“我的StringBuilder”页面上的所有复选框都具有正确的.Checked值。仍然相同,checkbox.Checked值始终为false,但哪个项目失败?cntl是否已填充(null或not null)?如果该值为空,则其他所有内容都将失败。仍然相同,checkbox.checked值始终为false项目失败为cntl,填充始终为false
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkBoxIsApproved" runat="server" Checked='<%#Eval("IsApproved")%>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>