开发者

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)


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.


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.


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);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜