Reapeter内嵌的Reapeter控件中,FindControl找不到控件?

fisher99 2008-07-17 10:30:18
如题,在内层的Reapeter控件中包含有Label服务器控件,但是在内嵌的Reapeter的ItemDataBound事件中 却报错:
Label lblAp=(Label)e.Item.FindControl("lblAvPrice2");
lblAp.Text="待定2"; ←这句出错,信息是“异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例”
...全文
75 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
fisher99 2008-07-17
  • 打赏
  • 举报
回复
问题解决了,谢谢大家。

原因出自这个事件绑定:
private void rptMonthList_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
Repeater rptLouP=new Repeater();
if(e.Item.ItemType==ListItemType.Item)
{
rptLouP=(Repeater)e.Item.FindControl("rptLoupan");
rptLouP.ItemDataBound+=new RepeaterItemEventHandler(rptLoupan_ItemDataBound);
}
else if(e.Item.ItemType==ListItemType.AlternatingItem)
{
rptLouP=(Repeater)e.Item.FindControl("rptLoupan2");
rptLouP.ItemDataBound+=new RepeaterItemEventHandler(rptLoupan2_ItemDataBound);
}
}

一开始我把【rptLouP.ItemDataBound+=new RepeaterItemEventHandler(rptLoupan2_ItemDataBound);
】这句放在了if else的外面,导致绑定事件到了非item行上
fisher99 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 silentwins 的回复:]
I'm sure that it's no need to find the control from its parent node.
Set a breakpoint at this sentense, check what lblAp is and what properties it has.
[/Quote]

设置断点跟进去,到语句【lblAp.Text="待定";】这句停住,提示【错误:标识符"lblAp"超出范围】
silentwins 2008-07-17
  • 打赏
  • 举报
回复
I'm sure that it's no need to find the control from its parent node.
Set a breakpoint at this sentense, check what lblAp is and what properties it has.
fisher99 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chinaicm 的回复:]
刚刚做了一个测试
用Control c = e.Item.FindControl("Label1");
这样就可以找到.
[/Quote]


我接着试了,这样
Control c=e.Item.FindControl("lblAvPrice");
Label lblAp=(Label)c;
lblAp.Text="待定";
还是不行啊

要是只写Control c=e.Item.FindControl("lblAvPrice");这句或是Label lblAp=(Label)e.Item.FindControl("lblAvPrice");这句是不会报错的,但是一给Text属性赋值“待定”就报错
chinaicm 2008-07-17
  • 打赏
  • 举报
回复
刚刚做了一个测试
用Control c = e.Item.FindControl("Label1");
这样就可以找到.
fisher99 2008-07-17
  • 打赏
  • 举报
回复
代码片段如下:三个方法都不行
private void rptLoupan_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
float avPrice=(float)Convert.ToDouble(DataBinder.Eval(e.Item.DataItem,"loupan_avpice"));
if(avPrice==0)
{
Label lblAp=(Label)e.Item.Parent.Parent.FindControl("rptLoupan").FindControl("lblAvPrice"); ←方法1 试验:找不到
Label lblAp=(Label)e.Item.Parent.FindControl("rptLoupan").FindControl("lblAvPrice"); ←方法2 试验:找不到
lblAp=(Label)this.rptMonthList.FindControl("rptLoupan").FindControl("lblAvPrice"); ←方法3 试验:找不到
lblAp.Text="待定";
}
else
{
Label lblAp=(Label)e.Item.FindControl("lblAvPrice2");
lblAp.Text="待定2";
}
}
}
mengjun5200 2008-07-17
  • 打赏
  • 举报
回复
你必须在外面的Reapeter中找里面的Reapeter,再从里面的Reapeter找label
silentwins 2008-07-17
  • 打赏
  • 举报
回复
Is there any header or footer in your inline repeater?

62,133

社区成员

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

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

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

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