Gil
Gil

Reputation: 275

Pass arguments from aspx to user control code behind

I tried to pass some labels of text from db to my user control inside a repeater, but without any success. It doesn't seem to pass my variable.

See discount variable for example

My Default.cs:

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

My Default.aspx:

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

My product user control:

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 ascx.cs:

public partial class ProductRow : Product {

}

My ProductRow user control ascx:

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

I must be doing something wrong, but i don't know what. Thanks in advance

Upvotes: 1

Views: 678

Answers (1)

Matt Evans
Matt Evans

Reputation: 7575

Don't think strDiscount is in scope. You would need to add ClassName='MyDefaultPage" attribute to your container pages page declaration. This allows you to treat it like a class.

Then in your control you need to get a reference to this page and its property like this in the control codebehind

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

Upvotes: 1

Related Questions