In custom user controls, we often add custom events, and then we use the delegate (delegate).
In development, it is often used to login. Then, we make the login interface into a control, it is more convenient to use it later.
The login interface is simple, do it casually, it has a "login" button. We need to click the "Login" button, it can perform our other pages defined by the login operation, then need to use the delegate, the login operation delegated to the "Login" button to execute. The following is the background code for the user control:
Code
public partial class WebUserControl : System.Web.UI.UserControl
{
public event EventHandler LoginClick;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
LoginClick(sender, e);
}
}
The event is decorated with a keyword, and its type is a delegate class. On the Aps.net page, we can add a method to the event:
Code
<uc1:WebUserControl ID="WebUserControl1" runat="server" OnLoginClick="Login_Click" /></div>
Code
protected void Login_Click(object sender, EventArgs e)
{
Response.Write("<script>alert('Logined')</script>");
}
Run the results as shown in figure: