System.NullReferenceException

microsoftAnson 2010-01-27 11:50:26
protected void Button1_Click(object sender, EventArgs e)
{
int userid = Convert.ToInt32(Session["users"]);
string Iid = string.Empty;
--错误在这里 for (int i = 0; i <this.Page.FindControl("RadioButton").Controls.Count; i++)
{

RadioButton rb = (RadioButton)Page.FindControl("RadioButton").Controls[i];
Iid = rb.Text;

}

vs.insert(Convert.ToInt32(Iid), userid);
Response.Redirect("Detiles.aspx");
}


this.Page.FindControl("RadioButton").Controls.Count 报错未将对象引用设置到对象的实例。

System.NullReferenceException
...全文
88 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
xray2005 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 microsoftanson 的回复:]
为什么 判断2次RadioButton? 你这样的  它不进for循环里面直接跳转添加方法里面去了。
[/Quote]

那说明this.Page.FindControl("RadioButton") 根本都没找到控件。你的页面上ID=“RadioButton"的控件吗?

另外注意FindControl传递的参数是控件的ID。

换成:
this.FindControl("控件的ID") 试试。后面的代码也注意是控件的ID
wuyq11 2010-01-27
  • 打赏
  • 举报
回复
实现什么功能
RadioButton rdb=Page.FindControl("RadioButton")as RadioButton;
ControlCollection.Count
为指定的 ASP.NET 服务器控件获取 ControlCollection 对象中的服务器控件数。
夏脑 2010-01-27
  • 打赏
  • 举报
回复
this.Page.Controls.Count
再转换
happy664618843 2010-01-27
  • 打赏
  • 举报
回复
或者放在一个panel中计算panel控件中控件数
happy664618843 2010-01-27
  • 打赏
  • 举报
回复
page.COntrols.count
microsoftAnson 2010-01-27
  • 打赏
  • 举报
回复
为什么 判断2次RadioButton? 你这样的 它不进for循环里面直接跳转添加方法里面去了。
xray2005 2010-01-27
  • 打赏
  • 举报
回复
int userid =Session["users"]==null?0:Convert.ToInt32(Session["users"]);
string Iid = string.Empty;
RadioButton rdbtn=this.Page.FindControl("RadioButton") as RadioButton ;
if(rdbtn!=null)
{
for (int i = 0; i <rdbtn.Controls.Count; i++)
{

RadioButton rb = rdbtn.Controls[i] as RadioButton ;
if(rb!=null)
Iid = rb.Text;

}
}
lihuaandzz 2010-01-27
  • 打赏
  • 举报
回复
没找到那个控件
microsoftAnson 2010-01-27
  • 打赏
  • 举报
回复
...还是不行 rdbtn 值还是为空..
fwacky 2010-01-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 microsoftanson 的回复:]
前台: <asp:RadioButton ID="RadioButton1" Text=' <%#Eval("Iid") %>' runat="server" /> <br />


后台: RadioButton rdbtn = this.Page.FindControl("RadioButton1") as RadioButton;

但是还是说  rdbtn  值为空...
[/Quote]
RadioButton rdbtn = this.Page.FindControl("RadioButton1") as RadioButton;
修改为:
RadioButton rdbtn = this.Page.Form.FindControl("RadioButton1") as RadioButton;
fwacky 2010-01-27
  • 打赏
  • 举报
回复


<asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RadioButton ID="RadioButton1" runat="server" Text="1" GroupName="110" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="2" GroupName="110" />
<asp:RadioButton ID="RadioButton3" runat="server" Text="3" GroupName="110" />
<asp:RadioButton ID="RadioButton4" runat="server" Text="4" GroupName="110" />
<asp:RadioButton ID="RadioButton5" runat="server" Text="5" GroupName="110" />

</asp:Panel>




string Iid = string.Empty;

foreach (Control ctl in this.Panel1.Controls)
{
if (ctl is RadioButton)
{
Iid = Iid + (ctl as RadioButton).Text;

}

}
this.TextBox1.Text = Iid.ToString();


这样做就可以!
microsoftAnson 2010-01-27
  • 打赏
  • 举报
回复
前台:<asp:RadioButton ID="RadioButton1" Text='<%#Eval("Iid") %>' runat="server" /><br />


后台: RadioButton rdbtn = this.Page.FindControl("RadioButton1") as RadioButton;

但是还是说 rdbtn 值为空...

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧