When using a Web user control, we often encounter a situation where the control needs to interact with the page containing it. There are many types of interactions, which are also divided into different layers. You can use viewstate, session, and even static variables. However, sometimes, when the control triggers a return, the event must be used for processing. When talking about events, it doesn't mean that the buttons or linkbuttons inside the Web user control trigger the return events, but regard a Web user control as a whole object, it has its own attributes, methods, and events. When a user control is embedded in a page file, it is hidden internally. However, it opens its own attributes, methods, and events. When an internal callback is triggered, depending on the status of the callback, it determines whether to send the callback to the parent page. At the same time, the parent page can know what information the child control can provide.
We know that with the event, we can pass the information that the user control knows to the parent page through eventargs, and then the parent page is processed based on the information. First, we will define an eventargs class. Assume that the user control contains three child controls, two text boxes, and one linkbutton. This linkbutton triggers the return event of the control. After the control is returned, it should tell the parent page that it can provide the content of the two text boxes, such as username and password. In the business logic layer, we create a class file and an eventargs class.
public class SubmitUserEventArgs : EventArgs{public SubmitUserEventArgs(string userName, string password){this.UserName = userName;this.Password = password;}public string UserName;public string Password;}
Then, you need to define a delegate to specify how to handle the submitted event.
public delegate void SubmitUserHandler(object sender,SubmitUserEventArgs e);
In this way, we can add an event to the background code file of the user control.
public event SubmitUserHandler SubmitUserEvent;
When will we throw this event to the parent page? This depends on the specific business. Now, if we trigger this event when the linkbutton in the control is clicked, we can fill in an eventargs object in the event processing method of the linkbutton and then stimulate this event:
SubmitUserEventArgs args = new SubmitUserEventArgs(tbUserName.Text, tbPassword.Text);SubmitUserEvent(this, args);
In this way, when the linkbutton triggers page callback, the entire control will promptly notify the page. In the HTML view of the page, you can add an event processing code to this control.
<uc1:UserControl ID="UC1" runat="Server" OnUserSubmitEvent="UC1_Submit">uc1>
Then, we can handle the ucloud submit event in the background as follows:
Protected void ucloud submit (Object sender, submitusereventargs e) {string username = E. username; string Password = E. password; // any code you need to process can be placed below ....}
In this way, the entire event is submitted completely. In fact, the above process is for saving space, but it does not explain some reasons. The reason for writing this is determined by the order in which the Asp.net page is loaded. The page always loads the subcontrol first and then the page information. In this way, this event can be triggered.