简体   繁体   中英

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. 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. 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.

If I remove the whole eval and just type a number in then it works fine.

Any help appreciated.

[UPDATE] : Issue Solved

I was using an EntityDataSource and this automatically binded to the repeater. 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(); 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:

<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.

Are you missing a ' at the end?

change following:

RequestId='<%# Eval("RequestId") %> />

to

RequestId='<%# Eval("RequestId") %>' />

您可以使用Repeater控件的ItemDataBound事件来实现此功能,以便您可以为控件设置动态属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM