【问题标题】:Pass arguments from aspx to user control code behind将参数从 aspx 传递到后面的用户控制代码
【发布时间】:2015-09-21 13:31:28
【问题描述】:

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

例如查看折扣变量

我的 Default.cs:

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

我的默认.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 %>
...

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

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    不要认为 strDiscount 在范围内。您需要将 ClassName='MyDefaultPage" 属性添加到您的容器页面页面声明中。这允许您将其视为一个类。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多