Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6475315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:41:23+00:00 2026-05-25T06:41:23+00:00

This is probably an easy one, but I can’t figure it out. argh… C#

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:41:24+00:00Added an answer on May 25, 2026 at 6:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably really easy but I can't seem to figure out how to
There is probably an easy to answer to this, but I can't even figure
This is probably a really easy one, but I can't seem to find the
Morning y'all This is probably an easy one but I barely got any sleep
This is probably an easy one, but I'm missing something I guess. The problem
I am sure that this is extremely easy and probably a one liner but
This is probably something quite easy to resolve, but for some reason, I can't
G'day folks This is probably an easy one for you lot, but i'm curious...
This is probably a really easy one... but after 4 days off from work
This seemed to me like an easy one, but I can't seem to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.