[英]Dynamically assigning a property in a usercontrol in an ASP.NET repeater control
I currently have a repeater control and inside the itemtemplate I have a usercontrol. 我目前有一个转发器控件,在itemtemplate里面我有一个usercontrol。 This usercontrol renders correctly, but I am trying to assign a dataitem to a property in the repeater control.
此用户控件正确呈现,但我尝试将数据项分配给转发器控件中的属性。
<asp:Repeater ID="Repeater1" DataSourceID="EntityDataSource" runat="server">
<ItemTemplate>
<uc1:Request ID="Request1" runat="server" RequestId='<%# Eval("RequestId") %>' />
</ItemTemplate>
RequestId is just an Int32. RequestId只是一个Int32。 It just doesn't assign it.
它只是没有分配它。
I can put the eval outside of the usercontrol just in the itemtemplate and it correctly outputs the right id. 我可以将eval放在usercontrol之外,只是在itemtemplate中它正确输出正确的id。
If I remove the whole eval and just type a number in then it works fine. 如果我删除整个eval并只输入一个数字,那么它可以正常工作。
Any help appreciated. 任何帮助赞赏。
[UPDATE] : Issue Solved [更新]:问题已解决
I was using an EntityDataSource and this automatically binded to the repeater. 我使用的是EntityDataSource,它会自动绑定到转发器。 It printed out all the information from the database on the screen without any codebehind.
它在屏幕上打印出数据库中的所有信息,没有任何代码隐藏。 But when I put in the code behind Repeater1.DataBind();
但是当我把代码放在Repeater1.DataBind()之后; it then started to work.
然后开始工作。
I don't know why, but hey it's solved. 我不知道为什么,但嘿,它已经解决了。 It now successfully passes the value through.
它现在成功传递了价值。 I imagine it has something to do with the page lifecycle.
我想它与页面生命周期有关。
If you just bind with repeater collection of int, you need use this: 如果只绑定int的repeater集合,则需要使用:
<uc1:Request ID="Request1" runat="server" RequestId='<%# Container.DataItem %>' />
And don't forget to call DataBind() for repeater or for Page where there is a repeater control. 并且不要忘记为转发器或具有转发器控件的Page调用DataBind()。
Are you missing a '
at the end? 是否缺少
'
在结束了吗?
change following: 改变如下:
RequestId='<%# Eval("RequestId") %> />
to 至
RequestId='<%# Eval("RequestId") %>' />
您可以使用Repeater控件的ItemDataBound事件来实现此功能,以便您可以为控件设置动态属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.