Fengying ASP. NET basic tutorial 8 user control programming-custom events

Source: Internet
Author: User
ArticleDirectory
    • User Control Page code
    • Event parameter code
    • Post Code of the control
    • Call page
    • Post Code
    • Running Effect
    • Interit
    • Static
    • Autoid

Since user controls can have their own methods and attributes, they can also have their own events. Through methods and properties, the user control responds to the webpageCode. However, when an event is used, it is opposite to the method and attribute. The user control notifies the webpage of some activities and then responds to the webpage code.

Sometimes, adding an event to a user control can solve the big problem. For example, if you log on to the user control, you can add a submission event to it. In this way, you can easily obtain the returned value, such as the user name and password (viewstate ). Of course, adding events to user controls is not that troublesome. Generally, you can define an event in the user control file and subscribe to the event on the host page (aspx. When the button is clicked and materials are submitted, an event is generated and a notification is published, so that the host page can receive information and process the event.

The following is a demonstration.

User Control Page code
 
<% @ Control Language = "C #" autoeventwireup = "true" codebehind = "logincontrol. ascx. cs" inherits = "aspnetteach4.logincontrol" %>
User name:<ASP: textbox ID= "Txtloginname" Runat= "Server"> </ASP: textbox>
 
Password:<ASP: textbox ID= "Txtloginpwd" Runat= "Server" Textmode= "Password"> </ASP: textbox>
    Asp: button   id   = "btnlogin"   runat   =" server "  text   = "button"   onclick   = "btnlogin_click"  />  

After the page is designed, create the custom object logineventcontrolargs class and add an eventmessage attribute to the class. This attribute is a read-only attribute used to return related user name and password information.

Event parameter code
 
Public ClassLogineventcontrolargs: eventargs
 
{
Public StringLoginname {Get; set ;}
 
Public StringLoginpwd {Get; set ;}
 
 
 
Public StringEventmessage {
 
Get {
 
ReturnDatetime. Now. tostring () +
 
"Account :"+ Loginname +
 
"--- Password :"+ Loginpwd;
 
}
 
}
 
}

After custom event parameters are defined, we also need to delegate the event signature. You can add the delegate to any place you like, usually in the same namespace.

 
Public Delegate VoidLogineventcontrolhandler (ObjectSender, logineventcontrolargs );
Post Code of the control
 
Public Partial ClassLogincontrol: system. Web. UI. usercontrol
 
{
 
// Define the event
Public EventLogineventcontrolhandler loginevent;
 
Protected VoidPage_load (ObjectSender, eventargs E)
 
{
 
}
 
 
 
Protected VoidBtnlogin_click (ObjectSender, eventargs E)
 
{
 
If(Loginevent! =Null){
 
Logineventcontrolargs ARGs =NewLogineventcontrolargs ();
 
Args. loginname = txtloginname. text;
Args. loginpwd = txtloginpwd. text;
 
Loginevent (This, ArgS );
 
}
 
}
 
}

Now we have a method for customizing events. Next we will use the page

Call page
<Body>
 
<Form ID= "Form1" Runat= "Server">
 
<Div>
 
<ASP: Label ID= "Label1" Runat= "Server" Text= "Label"> </ASP: Label>
<Uc1: logincontrol Runat= "Server" Onloginevent= "Logincontrol_loginevent" ID= "Logincontrol" />
 
</Div>
 
</Form>
 
</Body>
Post Code
 
Protected VoidLogincontrol_loginevent (ObjectSender, logineventcontrolargs ARGs)
 
{
If(ARGs. loginname ="Admin"& Amp; args. loginpwd ="123"){
 
This. Label1.text ="Login successful";
 
}
 
}
Running Effect

 

Dynamically load user controls in programming mode

 

In actual development, this situation often occurs, requiring the system to dynamically load related user controls according to certain conditions.

 
Protected VoidPage_load (ObjectSender, eventargs E)
 
{
 
Logincontrol c = page. loadcontrol ("~ /Logincontrol. ascx")AsLogincontrol;
 
Placeholder1.controls. Add (C );
 
C. loginevent + = logincontrol_loginevent;
 
 
 
}
 
 
 
 
Protected VoidLogincontrol_loginevent (ObjectSender, logineventcontrolargs ARGs)
 
{
 
If(ARGs. loginname ="Admin"& Amp; args. loginpwd ="123"){
 
This. Label1.text ="Login successful";
 
}
 
}

We can see that the method for dynamically adding controls is basically the same as that for winform.

However, the generatedSource codeThe Control ID is different from the ID on ASP.

We sometimes need to control Js. js compilation causes some errors if this ID is generated. So we can do this.

<Script Type= "Text/JavaScript">
 
Alert (<% = label1.clientid %>. innerhtml );
 
</Script>

Display Effect:

 

Clientidmode attribute

In addition to the above hard encoding method, we also have more elegant methods.

ASPnet provides us with three generated IDs.

Interit

By default, all controls on the page use clientidmode with the value of inherit, which indicates that the control will apply to the same clientidmode as its parent control, that is, This value specifies that the control generates an id like its parent control.

Static

Use the original control naming method

Autoid

Use the default 3.5 Generation Method

Next we will go to the second part of ASP. NET data access

Related Article

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.