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

本文关键字:用户 控制 代码 参数 aspx | 更新日期: 2023-09-27 18:13:57

我试图将一些文本标签从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) { 
    ... 
    }

My ProductRow user control asx .cs:

public partial class ProductRow : Product {
}

My ProductRow user control:

...
<%:Item.BonusGetValue%>% <%:strDiscount %>
...
我一定是做错了什么,但我不知道是什么。提前感谢

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

不要认为strDiscount在作用域中。您需要将ClassName='MyDefaultPage"属性添加到您的容器页面页面声明中。这允许你像对待一个类一样对待它。

然后在你的控件中,你需要在

后面的控件代码中获取对该页及其属性的引用
    var myDefault = (MyDefaultPage) this.Page();
    string discount = myDefault.strDiscount