我正在设置gridview,以便我可以选择多个事件并将其EventID添加到逗号分隔的字符串中.这将是一个订阅服务,所以我需要知道用户想要订阅哪些事件.
我使用模板字段添加一个Checkbox,我可以将它用作我想要的事件项的指示器.
所以gridview看起来像这样
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
PageSize="15"
ViewStateMode="Enabled" SelectedRowStyle-BackColor="Purple">
并且信息填充得非常好.
我检索所选项目的代码应该很简单但尚未运行.
private string GetCheckedEvents()
{
List outputString = new List();
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("eventSelected");
if (cb != null && cb.Checked)
{
outputString.Add(row.FindControl("EventID").ToString());
}
}
return string.Join(",", outputString);
}
虽然调试代码时,CheckBox设置正确并进入if语句,但然后点击单行代码并提出
An exception of type ‘System.NullReferenceException’ occurred in EventEmailerConfiguration.dll but was not handled in user code
尝试在调试器给出的对象资源管理器中导航是至少可以说是PITA.
我没有穿越到正确的位置吗?
如何提取我需要的信息?
解决方法:
您找不到BoundField,因为它不是真正的控件.你必须使用row.Cells [index] .Text:
outputString.Add(row.Cells[1].Text);
标签:c,asp-net,gridview
来源: https://codeday.me/bug/20190609/1206639.html