ASP.NET: everything on page or breaks in controls?

When you create a page visually divided into certain regions, do you break these regions into controls or put everything on the page and in one code?

If regions are divided into controls, how do you simplify the exchange of data between controls? By exchange, I mean just server-side data exchange.

Think of a page with a couple of boxes over a rather complex GridView. One cell dictates what the GridView displays, and the other allows you to do various things using the GridView. Both boxes must communicate with the net, but not with each other.

There must be at least a couple thousand lines for this code.

It would be useful to use any resources for making such constructive decisions.

thanks

+3
source share
7 answers

There are several reasons why you can use custom controls, for example:

  • Modular code : instead of one bloated page class, you can break the code into several user controls, each of which has less code. Thus, if your page has too much code to control, dividing it into user controls can reduce the size of each class.
  • : , , . , , , .

, , , , - . , , .

, , . , , - , , , .

, , , , . , , , .


http://www.codeproject.com/KB/user-controls/Page_UserControl.aspx
http://aspalliance.com/1461_Page_and_User_Control_Communication

+1

asp.net . , , . , . - , .

, , / , .

+4

Usercontrols , , , , . , , .

, . , - , BoxA ( ,) BoxB ( ) Grid ( gridview ), BoxA "DisplaySettingsChanged", , . , , DisplaySettingsChanged Grid.Refresh - .

, , - , , , , .

+1

, .

. . , .

0

, -

1: OK ///UAT, , ..

2: . / / /-. - .

1, , . , , ..

2 - , , , , / , ..

2. , .

ASP.net . , , , .

,

!!

0

, . . , html , , , . .

0

, , , , . , :

  • -.
  • , , . (, .)
  • aspx.

- , , , , ( ). , :

                ProxyNotifier
            /                  \
   Control1                      Control2

, Control1 Control2 ProxyNotifier. Control1 ProxyNotifer ProxyNotifer Control2.

: (: , ):

public static class ProxyNotifer
{
    // NOTE: This delegate should NOT be held in a static variable since
    // its session specific
    private static Action<string> NotifyEvent
    {
        get
        {
            if (!Session.ContainsKey["ProxyNotifyEvent"])
            {
                Session["ProxyNotifyEvent"] = null;
            }
            return Session["ProxyNotifyEvent"] as Action<string>;
        }
    }

    public static void Subscribe(Action<string> handler)
    {
        NotifyEvent += handler;
    }

    public static void Unsubscribe(Action<string> handler)
    {
        NotifyEvent -= handler;
    }

    public static void Invoke(string msg)
    {
        if (NotifyEvent != null) { NotifyEvent(msg); }
    }
}


public class Control1 : UserControl
{
    void SomethingHappened()
    {
        ProxyNotifyer.Invoke("Hello world!");
    }
}


public class Control2 : UserControl
{
    public Control2()
    {
        ProxyNotifer.Subscribe(Respond);
    }

    // ALWAYS unregister your event when your control is disposed, 
    // otherwise weirdness happens
    public void Dispose()
    {
        ProxyNotifier.Unsubscript(Respond);
        base.Dispose();
    }

    public void Respond(string msg)
    {
        lblMsg.Text = msg;
    }
}

Now Control1 and Control2 can communicate regardless of where they are on the page.

0
source

Source: https://habr.com/ru/post/1704857/


All Articles