将参数从aspx传递给后面的用户控制代码

时间:2015-09-21 13:31:29

标签: c# asp.net

我试图将一些文本标签从db传递到转发器内的用户控件,但没有任何成功。它似乎没有通过我的变量。

参见折扣变量,例如

我的Default.cs:

public String strDiscount { get; set; }
...
strDiscount = getCaptionFromDB("strDiscount");

我的Default.aspx:

<asp:Repeater ItemType="MyProduct" ID="ProductsGrid" runat="server">
    <ItemTemplate>
        <uc1:productRow runat="server" Item="<%#Item%>" strDiscount="<%#strDiscount%>" />
    </ItemTemplate>
</asp:Repeater>

我的产品用户控件:

public partial class Product : System.Web.UI.UserControl {

    public String strDiscount { get; set; }

    protected void Page_Load(object sender, EventArgs e) { 
    ... 
    }

我的ProductRow用户控件ascx.cs:

public partial class ProductRow : Product {

}

我的ProductRow用户控件ascx:

...
<%:Item.BonusGetValue%>% <%:strDiscount %>
...

我一定是做错了什么,但我不知道是什么。 提前致谢

1 个答案:

答案 0 :(得分:1)

不要认为strDiscount在范围内。您需要将ClassName ='MyDefaultPage“属性添加到容器页面页面声明中。这样您就可以像对待类一样对待它。

然后在您的控件中,您需要在控件代码隐藏中获得对此页面及其属性的引用

    var myDefault = (MyDefaultPage) this.Page();
    string discount = myDefault.strDiscount