Search code examples
c#asp.netrepeaterascx

Bind a value to a custom control inside a repeater


I can't see where I have gone wrong with this so I'm looking for some help.

I'm trying to use my custom control with parameters in a repeater. The parameter is always null when I debug it, even though the same expression returns data on the previous line of code.

Inside my control I have:

public string paddockName { get; set; }

And I am assigning it on my asp.net page as:

<mp:Historical runat="server" paddockName='<%# Eval("Name") %>' />

I made the previous line of code as per the below and it worked fine, so the expression is correct.

<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>

I've seen a previous question ASP.NET: Bind a value to a custom user control inside a repeater which is almost exactly the same as my problem, but the answer just says "I did a small test and I got it working if the ProductID is a string." and that doesn't help me figure out how they got it to stop being null when debugging.


Solution

  • The problem is that the data bound value is lost when the button postback happend. The property value is only set for the duration of the page request that has the DataBind() call. The button click is a new page request. The .NET controls store the bound value in ViewState so they are available on a post back. You can do the same thing by changing the way the property is stored.

    public string paddockName {
        get { return Convert.ToString(ViewState("paddockName")); }
        set { ViewState("paddockName") = value; }
    }