The application of a delegate (delegate) in a custom control

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.