简体   繁体   中英

How to pass a parameter to a usercontrol in a repeater

I have a usercontroll which accepts some parameter and I need to insert this usercontrol in repeater and pass a parameter from List<> to each one, I'm doing it like this:

<asp:Repeater ID="winnersRepeater" runat="server">
  <ItemTemplate>
    <uc:myControl ID="myControl" NodeId="<%# DataBinder.Eval(Container.DataItem,"Id")%>" runat="server" />        
  </ItemTemplate>
</asp:Repeater>

I do binding in PageLoad:

List<Winner> winners = new List<Winner>();
//...
winnersRepeater.DataSource = winners;
winnersRepeater.DataBind();

It doesn't work, I'm getting Error creating usercontrol... The server tag is not well formed.

How can I do it? thanks

The trick here is with quotes - you are using double quotes both to open and close the NodeId property here, and to enclose the string in the expression. You should use single quotes in one place, and double in the other.

You could handle the ItemDataBound event of the Repeater. In your handler you should be able to do FindControl("myControl") and pass to it the NodeId value without having to declare it in the mark-up.

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