William
William

Reputation: 1487

ASP.NET customized asp:label view state

I created a new class for a project. The below is a very slightly simplified version.

public class CustomLabel : Label
{
    public string ItemId { get; set; }

    protected override void Render(HtmlTextWriter writer)
    {
        if (!Page.IsPostBack)
            LoadText();
        base.Render(writer);
    }

    protected void LoadText()
    {
        this.Text = "This is a test";
    }
}

The problem I'm having is that the Text property is not lasting through postbacks. Even if I manually enable viewstate though the tag on the ascx page. Can the custom label tag not have viewstate? I would hate to have to load the text on every page load unnecessarily.

Upvotes: 0

Views: 270

Answers (1)

IUnknown
IUnknown

Reputation: 22478

Move LoadText(); method call to OnPreRender method

Upvotes: 1

Related Questions