views:

97

answers:

3

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)

A: 

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.

Mitchel Sellers
yes this would work, but this requires more code that i need to add in the hosting page (right now, beside the event subscribing the hosting page doesn't interact with controls) it will requre to loop through controls and finding them on the page. Is it better then saving onre integer ins session so both controls can read it?
toraan
Yes, see my update to this post as to why (Was too long for a comment)
Mitchel Sellers
so looping for usercontrols in hosting page (I need to find all user controls on my page) is the best option in my scenario?
toraan
I would believe so from a performance perspective
Mitchel Sellers
A: 

The container page has a property in viewstate that stores the state. When a user clicks on one of the pager, it raises an event that is handled by the container page. This then loops through the n user controls and calls a public method on those controls.

Raj Kaimal
A: 

You can build a quick modified version of the Observer Pattern. I would suggest building a manger control on the pages. But if you don't want to modify the page, here is a quick solution.

You can create a static method that will notify all of the same type of controls. By Calling their Update Method. Feel free to pass what ever data you need.

protected void control_event(object sender, EventArgs e)
{
    UpdateAllControls(page);
}


public static void UpdateAllControls(Control parent /* can be Page */)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.GetType() == this.GetType())
                    ((MyType)).Update()
                if (c.HasControls())
                    controls = GetAllControls(controls, t, c);
            }
        }
Glennular
This solution would work, however, i would caution against the recursion, if not necessary. if you have a lot of controls this can be an expensive process. If the user controls are always just added to the page, skip the recursion part of the process.
Mitchel Sellers
This is assuming you have no knowledge of how the page is set up. And yes I agree, it is a slow method. Register each control with an Observer would be the best solution.
Glennular
How the code above is different from the observer?
toraan
Do you mean to add and implement an interface and a subject class that will mantain the observers?All this just for passing int value between 2 user controls?
toraan