gdbj
gdbj

Reputation: 17485

asp.net my list is disappearing on postback - can't find what I'm missing?

This is probably an easy one, but I can't figure it out. argh...

C# asp.net application with a custom user control. My custom control is a simple list of images wrapped in a link to be used as a navigation tool for the parent page.

When I click a link, it does a postback: my click event does not seem to fire, and when the page reloads, the list of images disappears.

I have EnableViewState="true" in both the parent page and the custom control. It is my understanding that this would be enough to preserve my list of options? If I reload the list each time and rebind my event handler, I would think then that the event would be lost, correct?

Here is (abbreviated code) where I add the controls to my panel.

    public void LoadMediaOptions() {

      loop through a datareader {

        LinkButton b = new LinkButton();
        b.Click += new EventHandler(navOption_Click);
        b.Attributes.Add("mediaID", mediaID.ToString());

        HtmlImage i = new HtmlImage();
        i.Src = strThumbLink;
        i.Width = 120;
        i.Height = 90;
        b.Controls.Add(i);

        _loadedMediaHtml.Add(b);
        panMediaOptions.Controls.Add(b);
     }
  }

This method is currently only being called once in the parent page in the Page_Load method:

protected void Page_Load(object sender, EventArgs e)
{
  try
  {        
    if (!Page.IsPostBack)
      uxMediaNav.LoadMediaOptions();
  }
  catch (Exception ex)
  {
    throw ex;
  }
}

After the page posts-back, there are no media options loaded for me. My understanding of the EnableViewState is that it would preserve the elements I have loaded dynamically, but this is not the case.

Upvotes: 1

Views: 2222

Answers (2)

Dave
Dave

Reputation: 77

The solution is simple: put the changeControl in the Page_Load method.

protected void Page_Load(object sender, EventArgs e)
    {
        changeControl((UserControl)LoadControl("~/user-control.ascx"));
    }

Upvotes: 0

TheCodeKing
TheCodeKing

Reputation: 19220

You are constructing your controls too late in the page life cycle. ViewState is loaded after init and before Load. See here for the page life cycle.

This means that your custom control is unable to persist any state as the controls do not yet exist when load state occurs. Move your control construction to PreInit and it should work.

Control events fire after Load but before onLoadComplete. The event is actually passed in the request and matched to a control based on ID. Provided the control can be found by the time control events are triggered then the event will fire. Hence events generally do not rely on ViewState, with the exception of change events. Change events do because ASP.NET needs to be able to compare the original value persisted in ViewState with the current request value in order to know whether to fire the event.

To ensure any change events fire you again need to make sure the controls within your custom control are created on PreInit.

Upvotes: 1

Related Questions