3

About 5 months ago I was tasked with creating a new intranet site for my current employer as the old one is a nightmare to work with. The site uses multiple .NET languages (classic asp, VB, and C#) with multiple .NET frameworks (1.0, 2.0, 3.5, few places with 4.0). Simple changes that should only take an hour to implement and test would take days just to implement.

The new intranet sites content is controlled through user controls that are loaded dynamically at load time based on the page you are on and the access level you have. Each user control has a specific task and does not affect any other user control on the page.

About 3 weeks ago my database guy (was an application developer at his last job) pitched this idea to the middle management that the user controls could talk to each other and affect the selections available in each user control (all this without my knowledge).

At first, I didn't think it was possible when I heard. Then, everything I read about having user controls communicate with each other indicated that the user controls had to know about each other and that wasn't possible since all of the user controls are load at runtime based off of the access level you have. I found a solution last week were I could have a user control fire a custom event handler and have my other user controls listen for that specific custom event handler.

Now, today, I was asked if I could add filtering to the contact management part of the site that lists all of our clients similar to how Ebay has there filters on the left that allows you to drill down farther into the results returned. For example, you search "flat screen tvs". Ebay will list all results that match you search and on the left you can select the size range or the brands to narrow down the results.

On the page I setup I load 3 user controls to handle the criteria and the results. Control1 has all of the basic search criteria (ex. industry, region state, ect), control2 has the filters for drilling down the results from control1. Control3 displays the clients based off the criteria in control1 (so control1 fires an event that control2 and control3 hear and they both display the results based on control1). Now I select the criteria from control2 and fire the event that control3 can hear and displays the results.

All of this works, the problem I am having is that the controls in control2 are built dynamically and when the event in control1 is fired -> then control2 posts back to fire the event for control3 to hear I lose all the dynamic controls in control2 as the controls can't be recreated in the Page_Init because the values passed in from the custom event in control1 no longer exists because control2 did the postback and the event from control1 is only fired when control1 postsback. What is the best way to store the values passed in to control2 from control1's custom event or get control1 to repass the values when control2 posts-back so I can recreate the dynamic controls in control2?

Note: I tried using sessions but had trouble reassigning values from control1 after the first search. I believe the reason they don't work is due to the way I have control1 setup and the creation of dynamic controls in control2 is skipping over getting the session values.

I thank all of you in advance for your (hopefully) helpful responses.

Update
Turns out that the way I was loading my usercontrols at runtime on my default page is the reason why the dynamic control in 'control2' were not being recreated when 'control2' posted back. My default page loaded the usercontrols in the 'page_load' instead of the 'page_init' (must have forgot to move the loading of usercontrol to the 'page_init' like all my other pages). Made the switch and the dynamic controls are recreated on postback.

The only issue that I had after moving my code from the 'page_load' to the 'page_init' was that the 'checkbox' controls would be unchecked on postback even though I checked them. I was able to over come this with a few session variables.

John Saunders
  • 160,644
  • 26
  • 247
  • 397

1 Answers1

0

This is a common problem.

Only controls dynamically created in your page_init event can survive a postback.

During page_init, dynamically created controls become part of the DOM, and thereby have sessionstates. If you can re-factor your code to fire the dynamic control creation during page_init, your controls should survive.

Update:

I realize from your comments and post that you're reluctant to use Sessions. Problem is that Sessions are the ONLY way to save your controls.

  • One way I dealt with this case was to create a Class Object with Lists of Controls. When I came back to the page, if the Object existed I used it as a default.
  • Second way I approached this was to save the search criteria in Session and feed the criteria to my dynamic control creation method.
Dave Alperovich
  • 32,320
  • 8
  • 79
  • 101
  • I am aware that I need to recreate the control during page_init. The problem I have is the values I need in order to recreated the exact same controls based on the values control1 passed to control2 are lost when control2 postsback. I am looking for the best way to store the values passed into control2 from control1 so I can recreate the controls in control2 when control2 postsback. – user2045365 Feb 06 '13 at 05:34
  • I see. went thru this too. I understand you said you couldn't use Session. I did. I created a complex Class with List and stored that list. I checked for it to be non-null on return, and if it was populated, i used it to recreate my report. Not flashy but effective. – Dave Alperovich Feb 06 '13 at 05:36
  • read my updated post. It's obviously not what u want to hear, but its the only way I've heard of (with a lot of searching). – Dave Alperovich Feb 06 '13 at 05:41
  • I didn't say I couldn't use sessions, I said they didn't work the way I tried to use them. I will see if storing the list of controls in the sessions works. – user2045365 Feb 06 '13 at 06:03