Sharing data between usercontrols

On my page, I have n-userControls (the same control) I need to communicate between them (to be more specific, I need to pass a single value).

I do not want to attract a hosting page for this.

The controls act as “pagers” and interact with the page data on the hosting page through events to which the hosting page is subscribed.
Therefore, when the user clicks on one of the pagers and changes it, the other control should know about it and change it accordingly.

I cannot use VieState because viewstate for each control as well as controlstate.

Can I use Session for this? (the session is shared, and there is only one value that I need to save)

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

+5
source share
3 answers

Personally, there is no “easy” way to do this without doing it through a control page or event.

From what you say that I would foresee, there would be something like this. Suppose the two controls A and B are your pager controls.

"PageSelectionChanged" , , , " ".

→ , , .

?

, , "" .

  • , , ( 20 ).
  • 1, , (! ispostback) "" , .
  • , SQL Server -, .
  • -, (4 , ),
  • , , 1 .

: , , , , , , / , , , , .

+1

viewstate, . , , . n .

0

You can create a quick modified version of the Observer template. I would suggest creating control over the manger on the pages. But if you do not want to change the page, this is a quick fix.

You can create a static method that will notify all controls of the same type. Call the method to update them. Feel free to transfer what 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
source

All Articles