Using c# in Web Forms to passing parameter to user control

你说的曾经没有我的故事 提交于 2019-12-11 18:00:59

问题


From an aspx page, I am trying to display a user control for each item in a collection, but the C# seems to be ignored when tryign to set the UserControl parameter:

<%foreach (Fetus item in this.pregnancy.Fetus) {%>
    //this returns a GUID:
    "<%= item.Id.ToString() %>" 

    //this does not work, returns the characters between "" like < %= item.Id.ToString()%>:
    <uc1:AntepartumCTGChart runat="server" ID="AntepartumCTGChart" FetusId="<%= item.Id.ToString()%>" />
<% } %>

I would expect this to work, what's wrong?


回答1:


You have to use a data binding expression

<uc1:AntepartumCTGChart runat="server" ID="AntepartumCTGChart" FetusId='<%# item.Id.ToString()%>' />

But you have to call DataBind() in code behind for that to work.

You can also use a Repeater

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <uc1:AntepartumCTGChart runat="server" ID="AntepartumCTGChart" FetusId='<%# Eval("id").ToString()%>' />
    </ItemTemplate>
</asp:Repeater>

And then bind data to it in code behind

Repeater1.DataSource = pregnancy.Fetus;
Repeater1.DataBind();


来源:https://stackoverflow.com/questions/48811256/using-c-sharp-in-web-forms-to-passing-parameter-to-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!