SoFunction
Updated on 2025-04-07

[.net] Free-to-cooperation and communication within the page

.NET pages seem to be a whole, but they may be combined from many different regions. The most distinctive examples are the master pages and user controls that are often used.
However, in order to form a whole, communication between controls and transmission of values ​​are indispensable. This article is something that I have summarized in the process of continuous use. There are fallacies or better solutions. Please also propose it.
In the process of writing this article, I made some examples to support it. The purpose of these examples is to change the value of a Label in "B" in "A".
What is the significance of this approach?
For example, if you contain a GridView in the masterpage, it changes some data in aspx, and these data affect the rendering content of the GridView in the masterpage, then it is necessary for you to notify the masterpage to be updated in aspx. The purpose of this article is to talk about how to notify its updates.
This article includes the following parts:
1. Communication between aspx and ascx
2. Communication between master and aspx
3. Ascx in master communicates with aspx in master
4. Communication between ascx and ascx

1. Communication between aspx and ascx

A simple example, in this example, there are two files in total: and both files contain a TextBox, Label and a Button
Here we use A and B to shorten the former and the latter.
The required effect is: clicking the Button in A can assign the value in TextBox in A to the Label in B; conversely, clicking the Buton in B to assign the value in TextBox in B to the Label in A.
That is to say, they can change the content of each other's controls.
First of all, A->B
This is very simple. Just write a public method in Ascx and call it in aspx.

[Copy to clipboard] [ - ]CODE:
//This is the method in ascx, which assigns the value of parameter _value to Label.
    public void setSelect(string _value)
    {
         = _value;
    }
Call it directly in aspx

[Copy to clipboard] [ - ]CODE:
    protected void btnSet_Click(object sender, EventArgs e)
    {
WebUserControl3_1.setSelect();// WebUserControl3_1 is the ID of the user control
    }
Maybe everyone doesn't understand it very well, but in short, you can call the public method in ascx in ascx and pass in the parameters you want to pass, which is OK.

If reversed, by B->A.

Change a Label in aspx in ascx, and only change the method

[Copy to clipboard] [ - ]CODE:
    Label lblMessage= (Label)("lblMessage");
     = ;
In other words, you can find the control ID of the current aspx page in ascx. Once found, just assign a value directly. Anyway, as long as you find this control, it will be used as yours.

2. Communication between master and aspx

Accessing things in aspx in master is also a search control, which is similar to finding things in ascx in ascx

[Copy to clipboard] [ - ]CODE:
    Label lblMessage= (Label)("lblMessage");
     = ;
On the other hand, aspx can call the public method of masterpage

[Copy to clipboard] [ - ]CODE:
MasterPage master = (MasterPage);//The type converted to masterpage
();//Calling the masterpage method
3. Ascx in master communicates with aspx in master

From ascx in master to aspx in master, you need to search for ContentPlaceHolder through master, and then search for Label

[Copy to clipboard] [ - ]CODE:
    MasterPage master = (MasterPage);
    Label _lblMessage = (Label)("ContentPlaceHolder1").FindControl("lblMessage");
    _lblMessage.Text = ;
If aspx wants to contact the masterpage's ascx, you must first get the master, then look for ascx, and then look for the Label.

[Copy to clipboard] [ - ]CODE:
    MasterPage master = (MasterPage);
    Label lblMessage=(Label)("WebUserControl4_1").FindControl("lblMessage");
     = ;
The principle of mutual communication between the two parties is the same.

4. Communication between ascx and ascx

This should be the most common situation. The projects I did some time ago were divided into two areas. The user was engaged in activities in Area A and the B recorded the information about his activities. These two areas were user controls.
One way is to look for Aspx from it, then search for Label.
This direction works theoretically, but I didn't try it, because this method must consider the ID of control B in A, but I don't want to have any relationship with the ID.
So I use the interface.
Suppose there is an existing control that you want to manipulate.
Create a new one in app_code
content:

[Copy to clipboard] [ - ]CODE:
public interface IUserControl1
{
    void setSelect(string value);
}
Inheriting the interface and implementing its methods.

[Copy to clipboard] [ - ]CODE:
public partial class UserControl_WebUserControl1 : , IUserControl1
{
    public void setSelect(string _value)
    {
         = _value;
    }
}
Then directly in the call method

[Copy to clipboard] [ - ]CODE:
IUserControl1 userControl1 = (IUserControl1)("WebUserControl1_1");//Convert to the interface
();//setSelect is the method in, call it.
This is actually a good way. If you do this, communication between other types of controls should be possible. During those days, I was still complacent. Through this example, I also found that the interface is indeed not simple, it is really a "interface".

The introduction of .net's partial class is used by me to implement one in each file and finally merge it together. Through these little tricks, we can connect the parts and become a real "whole".

My blog:.
Its related procedures are attached to the attachment.