Add properties and events for Web user controls in Asp.net

Source: Internet
Author: User
In early 1990s, Microsoft Program The Active Server Pages (ASP) provided by members revolutionizes web programming. It can use a very easy-to-use model to dynamically generate HTML on the Web server and easily implement access to the database. At that time, this was an attractive technology, many Web sites on the Internet are now written in ASP. My colleagues and predecessors are even more familiar with ASP. After so many years of experience, his success is evident.
However, the technology is constantly evolving. In the words of a net expert, the status of WEB programming is still lagging behind. Therefore, Microsoft proposed the second generation programming model-web forms. The web form model is part of Asp.net, and Asp.net is part of the. NET Framework. His programming model is based on events. It is more like using Windows Forms for programming. This is also an important reason why I decided to use it, I read some books in this area and wrote this article. Article The goal is to share your experience with Asp.net beginners and colleagues who have not added custom events to user controls.
To put it bluntly, let's create a user control first. Here we will use a simple logon user control for demonstration.
Let's take a look at the front-end of the user control. Code (Loginoutcontrol. ascx file ):
<% @ Control Language = "C #" autoeventwireup = "false" codebehind = "loginoutcontrol. ascx. cs" inherits = "ZZ. loginoutcontrol" targetschema = "http://schemas.microsoft.com/intellisense/ie5" %>
<Table id = "Table1" style = "font-size: 9pt; width: 183px; Height: 125px" cellspacing = "1"
Cellpadding = "1" width = "183" align = "center" border = "1">
<Tr>
<TD Height = "20">
<Asp: Label id = "labeluser" runat = "server"> User: </ASP: Label>
<Asp: textbox id = "textboxusername" width = "128px" runat = "server"> </ASP: textbox> </TD>
</Tr>
<Tr>
<TD Height = "20"> <font face = "">
<Asp: Label id = "labelpassword" runat = "server"> password: </ASP: Label>
<Asp: textbox id = "textboxpassword" width = "128px" runat = "server" textmode = "password"> </ASP: textbox> </font> </TD>
</Tr>
<Tr>
<TD align = "center" Height = "20"> <font face = "">
<Asp: button id = "buttonlogin" width = "50px" text = "login" runat = "server"> </ASP: button>
<Asp: button id = "buttonlogout" width = "49px" text = "logout" runat = "server"> </ASP: button> </font> </TD>
</Tr>
</Table>
We simply put two labels, two Textbox, two buttons, and an HTML table.
The next step is to add code to the loginoutcontrol. ascx. CS file.
First define a delegate, where the loginouteventargs class is inherited from the eventargs class,
Public Delegate void loginoutclickhandler (Object sender, loginouteventargs E );
I think it is more appropriate to put this delegate Out Of The loginoutcontrol class.
Next, the loginoutclick event is declared for the control, as follows:
Public event loginoutclickhandler loginoutclick;
In addition, in order to better use attributes, the language enumeration is added,
Private language;
Of course, external access is made through the public language LG {Get; set;} attribute. The purpose is to change or obtain the display of the current control.
The next step is to define the onloginoutclick function to trigger the control event by clicking the event processing function.
The complete code is as follows:
Namespace ZZ
{
Using system;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

// Define the proxy
Public Delegate void loginoutclickhandler (Object sender, loginouteventargs E );
Public class loginoutcontrol: system. Web. UI. usercontrol
{
Protected system. Web. UI. webcontrols. Button buttonlogin;
Protected system. Web. UI. webcontrols. textbox textboxusername;
Protected system. Web. UI. webcontrols. textbox textboxpassword;
Protected system. Web. UI. webcontrols. Button buttonlogout;
Protected system. Web. UI. webcontrols. Label labeluser;
Protected system. Web. UI. webcontrols. Label labelpassword;
Public event loginoutclickhandler loginoutclick;
Private language;
// Method
Public void changelanguage (language)
{
This. lg = language;
}
// Attributes
Public language LG
{
Set
{
If (value! = This. Language)
{
If (value = language. English)
{
This. labeluser. Text = "User :";
This. labelpassword. Text = "Password :";
This. buttonlogin. Text = "login ";
This. buttonlogout. Text = "logout ";
}
Else
{
This. labeluser. Text = "User :";
This. labelpassword. Text = "Password :";
This. buttonlogin. Text = "login ";
This. buttonlogout. Text = "logout ";
}
}
}
}
Private void page_load (Object sender, system. eventargs E)
{
If (this. labeluser. Text = "User :")
This. Language = language. English;
Else
This. Language = language. Chinese;
}
Private void onloginoutclick (Object sender, loginouteventargs E)
{
If (loginoutclick! = NULL)
Loginoutclick (this, e );
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}
Private void initializecomponent ()
{
This. buttonlogin. Click + = new system. eventhandler (this. buttonlogin_click );
This. buttonlogout. Click + = new system. eventhandler (this. buttonlogout_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void buttonlogin_click (Object sender, system. eventargs E)
{
Onloginoutclick (this, new loginouteventargs (loginclicktype. Longin, customvalidate (this. textboxusername. text, this. textboxpassword. Text )));
}
Private void buttonlogout_click (Object sender, system. eventargs E)
{
// The logout code is omitted.
Onloginoutclick (this, new loginouteventargs (loginclicktype. longout, true ));
}
// Verify the Function
Private bool customvalidate (string username, string password)
{
// The verification code is omitted. If the verification code passes
Return true;
}
}
}
Another file defines enumeration and parameter classes:
Using system;
Namespace ZZ
{
Public class loginouteventargs: eventargs
{
Private loginclicktype type;
Private bool result;

Public loginouteventargs (loginclicktype, bool result): Base ()
{
This. type = type;
This. Result = result;
}
Public loginclicktype type
{
Get {return this. type ;}
}
// Operation result,
Public bool result
{
Get {return this. Result ;}
}
}
// Operation Type
Public Enum loginclicktype: int
{
Longin,
Longout
}
// Definition Language
Public Enum Language
{
Chinese,
English
}
}
Next let's take a look at the usage on the ASPX page.
Create a new default. aspx page and drag a loginoutcontrol user control to it.
<% @ Register tagprefix = "uc1" tagname = "loginoutcontrol" src = "loginoutcontrol. ascx" %>
<% @ Page Language = "C #" codebehind = "default. aspx. cs" autoeventwireup = "false" inherits = "ZZ. Default" %>
<% @ Import namespace = "ZZ" %>
<HTML>
<Head>
<Title> webform1 </title>
</Head>
<Body>
<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Uc1: loginoutcontrol id = "loginoutcontrol1" runat = "server">
</Uc1: loginoutcontrol>
<Asp: Label id = "labelmsg" runat = "server"> </ASP: Label>
<Asp: dropdownlist id = "dropdownlist1" runat = "server" autopostback = "true">
<Asp: listitem value = "0" selected = "true"> Chinese </ASP: listitem>
<Asp: listitem value = "1"> English </ASP: listitem>
</ASP: dropdownlist> </font>
</Form>
</Body>
</Html>
Add events and attributes to the background code.
Although loginoutcontrol1 is added to the foreground, the background Code does not generate the protected loginoutcontrol loginoutcontrol1; this statement is strange to me, no matter whether you add it first.
Register the loginoutclick event in the page_load event:
This. loginoutcontrol1.loginoutclick + = new loginoutclickhandler (loginoutcontrolpolicloginoutclick );

The complete code is as follows:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;

Namespace ZZ
{
Public class default: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. Label labelmsg;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1;
Protected loginoutcontrol loginoutcontrol1;
Private void page_load (Object sender, system. eventargs E)
{
// Register a user control event
This. loginoutcontrol1.loginoutclick + = new loginoutclickhandler (loginoutcontrolpolicloginoutclick );
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}
Private void initializecomponent ()
{
This. dropdownlist1.selectedindexchanged + = new system. eventhandler (this. dropdownlist+selectedindexchanged );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void loginoutcontrolpolicloginoutclick (Object sender, loginouteventargs E)
{
Switch (E. type)
{
Case loginclicktype. Longin:
This. labelmsg. Text = "you have clicked the logon button. Operation Result:" + E. Result. tostring ();
Break;
Case loginclicktype. longout:
This. labelmsg. Text = "you have clicked the Logout button. Operation Result:" + E. Result. tostring ();
Break;
}
}
Private void dropdownlistpolicselectedindexchanged (Object sender, system. eventargs E)
{
This. loginoutcontrol1.lg = (language) This. dropdownlist1.selectedindex;
// This. loginoutcontrol1.changelanguage (language) This. dropdownlist1.selectedindex );
}
}
}
When you select the drop-down box list on the front-end to change the language of the control, the LG attribute is used here. However, a method changelanguage is added to achieve the same function. In addition, you can click the login or logout button to trigger the loginoutclick event and assign values to the labelmsg. Text attribute on the page to obtain the operation result.
In summary, user controls bring high development efficiency and reusability to programmers, and greatly improve the performance. previously called ASP +, in fact, I think Asp.net has no direct connection with ASP. In addition, my friends who want to work as applications prefer code separation when developing web programs, so that the structure is clearer and can be modified and managed. Compared with the ASP program, it is compiled and introduces the object-oriented design idea, which inevitably brings about its complexity. To develop a high-level Asp.net program, for the design of patterns and the division of hierarchies, we should pay more attention to it here. In short, it is more like compiling a Windows form program than writing a VB script.

 

(Source: Belden network College)

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.