在Web窗体中使用c#将参数传递给用户控件 [英] Using c# in Web Forms to passing parameter to user control

查看:235
本文介绍了在Web窗体中使用c#将参数传递给用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从apx页面,我试图显示集合中每个项目的用户控件,但是当尝试设置UserControl参数时,C#似乎被忽略了:

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?

推荐答案

您必须使用

但是您必须在后面的代码中调用DataBind()才能起作用.

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

您还可以使用中继器

<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();

这篇关于在Web窗体中使用c#将参数传递给用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆