Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share data between usercontrols

On my page I have n-userControls (same control) I need to communicate between them(to be more specific I need to pass one in value) .

I don't want to involve the hosting page for that.

The controls acts as "pagers" and interact with the paged data on the hostin page via events that hosting page is subscribed to.
So when user click on one of the pager and changes it's state, the other control should know about it and change itself accordingly.

I can not use VieState because viewstate is per control and so is the controlstate.

Can I use Session for that? (session is shared and there is only one value that i need to store)

Or maybe there is something better I can use? (no QueryString)

like image 439
toraan Avatar asked Mar 26 '10 21:03

toraan


1 Answers

Personally there isn't an "easy" way to do this without doing it through the controlling page, or an event.

From what you are saying what I would envision would be something like this. Assuming two controls A and B that are your pager controls.

The containing page subscribes to the "PageSelectionChanged" event on both controls, in response to that event it updates the data, which you already have, AND it enumerates through all pager controls setting the "Current Page" value.

You already have event plumbing in place for communication from control -> page, use what you already have built.

Why Not Session?

I was asked in the comments if this would be better than session, and the answer is yes, for a number of reasons.

  1. Session information, unless explicitly cleaned up exists for the duration of a users session (typically 20 minutes)
  2. Becase of number 1, you would need to add items to the page, for if(!ispostback) to "clear" the session variables so that the user didn't start on a different page.
  3. Future application growth, session information has to be moved out of process to SQL Server or otherwise to work in a web farm environment, for this I try to avoid it as well.
  4. Using session stores this information in memory on the webserver, although small (4 bytes if integer) it can add up and is un-necessary
  5. Depending on the nature of your updates, you cannot ensure control order with session alone to ensure that 1 control forces an update to all controls.

There are other solutions, the solution similar to the one posted above that does a recursive look at the page, but you have to be careful with that to ensure that you do not get into a looping/endless recursion situation, in addition, if you have a lot of controls on the page, it can add a lot of overhead to constantly loop through everything.

like image 169
Mitchel Sellers Avatar answered Oct 29 '22 16:10

Mitchel Sellers