这是六楼的方法执行结果: “/web”应用程序中的服务器错误。 --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 50: // } 行 51: 行 52: for (int i = 0; i < cb.Items.Count; i++) 行 53: { 行 54: if (cb.Items[i].Selected) 源文件: d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs 行: 52 堆栈跟踪: [NullReferenceException: 未将对象引用设置到对象的实例。] Page_Default.Button1_Click(Object sender, EventArgs e) in d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs:52 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 -------------------------------------------------------------------------------- 版本信息: Microsoft .NET Framework 版本:2.0.50727.4927; ASP.NET 版本:2.0.50727.4927
这是监视里面的: cb.Items.Count “((System.Web.UI.WebControls.ListControl)(cb))”为 null int
CheckBoxList cb = Page.FindControl("CheckBoxList1") as CheckBoxList; for (int i = 0; i < cb.Items.Count; i++) { if (cb.Items[i].Selected == true) { } } 我这里可以跑通
“/web”应用程序中的服务器错误。 --------------------------------------------------------------------------------未将对象引用设置到对象的实例。 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 行 59: 行 60: CheckBoxList cb = Page.FindControl("CheckBoxList1") as CheckBoxList; 行 61: for (int i = 0; i < cb.Items.Count; i++) 行 62: { 行 63: if (cb.Items[i].Selected == true) 源文件: d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs 行: 61 堆栈跟踪: [NullReferenceException: 未将对象引用设置到对象的实例。] Page_Default.Button1_Click(Object sender, EventArgs e) in d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs:61 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 -------------------------------------------------------------------------------- 版本信息: Microsoft .NET Framework 版本:2.0.50727.4927; ASP.NET 版本:2.0.50727.4927
c1.Text = "A";
c1.ID= "1";
this.p1.Controls.Add(c1);
CheckBox cb = new CheckBox();//定义控件类别 cb = (CheckBox)p1.FindControl("cb1");//查找刚才定义的控件ID
//在这里进行IF判断时会报出未将对象引用到实例
if(c1.Checked == true)
{}
你获取对象的时候干嘛还要new个新的?
{
if( ChkList . Items [ i ] . Selected )
{
lblResult . Text += ChkList . Items [ i ] .Text + " <br > " ;
}
}
可是new个新的在IF判断时它还是会报未将对象引用到实例
{
在这里进行IF判断时会报出未将对象引用到实例
if(cb.Items[j].Selected == true)//查找控件的哪些选项被选中了! {
//执行相关的操作!
Response.Write(cb.Text); } }
“/web”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 50: // }
行 51:
行 52: for (int i = 0; i < cb.Items.Count; i++)
行 53: {
行 54: if (cb.Items[i].Selected)
源文件: d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs 行: 52 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Page_Default.Button1_Click(Object sender, EventArgs e) in d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs:52
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.4927; ASP.NET 版本:2.0.50727.4927
cb.Items.Count “((System.Web.UI.WebControls.ListControl)(cb))”为 null int
CheckBoxList cb = Page.FindControl("CheckBoxList1") as CheckBoxList;
for (int i = 0; i < cb.Items.Count; i++)
{
if (cb.Items[i].Selected == true)
{ }
}
我这里可以跑通
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 59:
行 60: CheckBoxList cb = Page.FindControl("CheckBoxList1") as CheckBoxList;
行 61: for (int i = 0; i < cb.Items.Count; i++)
行 62: {
行 63: if (cb.Items[i].Selected == true)
源文件: d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs 行: 61 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Page_Default.Button1_Click(Object sender, EventArgs e) in d:\Work\景澜货物管理系统\动态页面\JLCMS\web\Page\Default.aspx.cs:61
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.4927; ASP.NET 版本:2.0.50727.4927
<table id="cb1" border="0">
<tr>
<td><input id="cb1_0" type="checkbox" name="cb1$0" /><label for="cb1_0">A</label></td>
</tr><tr>
<td><input id="cb1_1" type="checkbox" name="cb1$1" /><label for="cb1_1">B</label></td> </tr><tr>
<td><input id="cb1_2" type="checkbox" name="cb1$2" /><label for="cb1_2">C</label></td>
</tr><tr>
<td><input id="cb1_3" type="checkbox" name="cb1$3" /><label for="cb1_3">D</label></td>
</tr><tr>
<td><input id="cb1_4" type="checkbox" name="cb1$4" /><label for="cb1_4">E</label></td>
</tr><tr> <td><input id="cb1_5" type="checkbox" name="cb1$5" /><label for="cb1_5">F</label></td>
</tr><tr>
<td><input id="cb1_6" type="checkbox" name="cb1$6" /><label for="cb1_6">G</label></td>
</tr><tr>
<td><input id="cb1_7" type="checkbox" name="cb1$7" /><label for="cb1_7">H</label></td>
</tr>
</table></div>这是生成的HTML 你自己看着办 是否考虑JS来弄,。
<asp:CheckBoxList ID="chkContry" runat="server" RepeatDirection="Horizontal" AutoPostBack="true">
<asp:ListItem Text="北京" Value="北京" />
<asp:ListItem Text="上海" Value="上海" />
<asp:ListItem Text="广州" Value="广州" />
</asp:CheckBoxList>
</asp:Panel>后台 public string countryArr = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CheckBoxList countryList= pnChk.FindControl("chkContry") as CheckBoxList;
for (int i = 0; i < countryList.Items.Count; i++)
{
if (countryList.Items[i].Selected)
{
countryArr += countryList.Items[i].Text;
}
}
}
Response.Write(countryArr);
}
去掉!,因为页面刷新把你加载的都清空了。