Restore dynamic controls in a user control based on a selection in another user control

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 languages .NET

( classic asp

, VB

and C#

) with multiple .NET frameworks

(1.0, 2.0, 3.5, multiple locations with 4.0). Simple changes that take only an hour to implement and test would take only days to implement.

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

About 3 weeks ago, my database guy (was an app developer on his last assignment) pitched this idea to a middle control that user controls can talk to each other and influence the choices available in each user control (all without my knowledge ).

At first I didn't think it was possible when I heard. Then everything I read about custom controls interacting with each other indicates that the custom controls were supposed to know about each other and this is not possible since all custom controls are loaded at runtime based on access level which you have. I found a solution last week if I had a custom control triggering a custom event handler and my other custom controls listen to that 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 customers, similar to how Ebay has filters on the left, allowing you to go deeper into the returned results, For example, you are browsing "flat screen televisions". Ebay will display all the results that match your search and on the left you can select a range of sizes or brands to narrow down the results.

On the page I setup, I load 3 custom controls to handle the criteria and results. Control1

has all the basic search criteria (eg industry, state of the region, ect), control2

has filters for drilling results from Control1

. Control3

renders clients based on criteria in control1 (so control1 fires an event that control2

it Control3

hears, and they both render results based on control1 ). Now I select criteria from control2

and fire an event that Control3

can hear and display the results.

It all works, the problem is that the controls in are control2

built dynamically and when the event is fired in Control1

-> then control2

dispatched back to fire the event to Control3

hear that I am losing all dynamic controls in control2

since the controls cannot be recreated in Page_Init

. because the values ​​passed from the custom event in control1 no longer exist because it control2

did a postback and the event from Control1

is only triggered when control1 responds back. What is the best way to store the values ​​passed to control2

from a Control1

custom event, or get Control1

to retransmit the values ​​whencontrol2

feedback so I can recreate the dynamic controls in control2

?

Note. I tried using sessions but was unable to reassign the values ​​from control1 after the first search. I believe the reason they don't work is because I have a control1 setting and creating dynamic controls in control2

skips getting session values.

I thank you all for your (hopefully) helpful answers.

Update
It turns out that the default runtime way of loading my usercontrols in my page is the reason the dynamic control in "control2" was not being recreated when "control2" was sent back. My default page was loading usercontrols to "page_load" instead of "page_init" (maybe forgot to move usercontrol load to "page_init" like all my other pages). Made a switch and dynamic controls are recreated on postback.

The only problem I ran into after moving my code from "page_load" to "page_init" was that the "checkbox" items were not checked on postback even though I checked them. I was able to complete this with a few session variables.

+3


source to share


1 answer


This is a common problem.

Only controls dynamically created in your event page_init

can survive postback.

At run time, page_init

dynamically created controls become part DOM

and thus have sessionstates

. If you can reuse code to trigger dynamic control creation in time page_init

, your controls should survive.



Update:

I understand from your comments and posts that you are reluctant to use sessions. The problem is that sessions are the only way to keep your controls.

  • In one case, I was dealing with the creation of a class object with lists of controls. When I returned to the page, if the object existed, I used the default.
  • The second approach I came up with was to store the search criteria in the session and pass the criteria to the dynamic control creation method.
0


source







All Articles