To add properties and events to a Web user control in ASP.net

Source: Internet
Author: User
Tags define definition bool tostring
asp.net|web| Control

In the early 90, Microsoft had revolutionized web programming for Active Server Pages (ASP) provided by web programmers. It can use the very easy-to-use model to dynamically generate HTML on the Web server, and it is easy to access the database, at that time, this is an attractive technology, including the Internet now many Web sites are written in ASP, My colleagues predecessors are playing the Master of ASP, experience so many years without failure, visible his success.
However, technology is constantly evolving, quoting a net expert saying that the state of web programming is still lagging behind. So Microsoft proposes a second-generation programming model ――web form. The Web Forms model is part of the asp.net, and ASP.net is part of the. NET Framework. His programming model is event-based, and it's more like working on Windows Forms programming, which is one of the big reasons I decided to learn to use him, and I read some books about it, The purpose of this article is to share experience with fellow ASP.net beginners and peers who have not yet added custom events to user controls.
Less nonsense, let's start by creating a user control, here is a simple login user control to do the demo.
First take a look at the foreground code for the user control (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>
&LT;TD height= ">"
<asp:label id= "Labeluser" runat= "Server" > User:</asp:label>
<asp:textbox id= "Textboxusername" width= "128px" runat= "Server" ></asp:TextBox></TD>
</TR>
<TR>
&LT;TD height= "><font" face= "Song Body" >
<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>
&LT;TD align= "center" height= "><font face=" song Body ">
<asp:button id= "Buttonlogin" width= "50px" text= "Login" runat= "Server" ></asp:Button>
<asp:button id= "Buttonlogout" width= "49px" text= "Logoff" runat= "Server" ></asp:button></font></td >
</TR>
</TABLE>

We simply put two labels, two textbox, two button and an HTML table.
The next thing is to add code to the LogInOutControl.ascx.cs file.
First, define a delegate, where the Loginouteventargs class inherits from the EventArgs class,
public delegate void Loginoutclickhandler (Object Sender,loginouteventargs e);

I think it's more appropriate to put this delegate outside the Loginoutcontrol class.
Next, the Loginoutclick event is declared for the control, as follows:
public event Loginoutclickhandler Loginoutclick;

In addition, for better use of attributes, the language enumeration is added,
Private Language Language;

Of course externally through the public Language Lg {get;set;} property to access the. The purpose is to change or get the display of the current control.
The next thing is to define the control event trigger function Onloginoutclick, which completes the triggering of the user control event by clicking the event handler function on the button.
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;

Defining agents
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 Language;
Method
public void Changelanguage (Language Language)
{
This. Lg = language;
}
Property
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 #region the Web forms 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)
{
Logoff code omitted
Onloginoutclick (this,new Loginouteventargs (loginclicktype.longout,true));
}
Validation functions
private bool Customvalidate (string username,string password)
{
The validation code is omitted, assuming that the
return true;
}
}
}

Another file defines the enumeration and parameter classes:
Using System;
Namespace ZZ
{
public class Loginouteventargs:eventargs
{
Private Loginclicktype type;
private bool result;

Public Loginouteventargs (Loginclicktype type,bool result): Base ()
{
This.type = type;
This.result = result;
}
Public Loginclicktype Type
{
Get{return This.type;}
}
Operation Results,
public bool Result
{
Get{return This.result;}
}
}
Type of action
public enum Loginclicktype:int
{
Longin,
Longout
}
Definition language
public enum Language
{
Chinese,
中文版
}
}

Take a look inside the ASPX page to use.
Create a new Default.aspx page and drag a Loginoutcontrol user control to the top.
<%@ 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= "Song Body" >
<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 properties to the background code.
Although the LogInOutControl1 is added in the foreground, the protected Loginoutcontrol LogInOutControl1 is not generated in the background code; This statement, I feel very strange, regardless of first plus him.
Then register the Loginoutclick event in the Page_Load event:
This. Loginoutcontrol1.loginoutclick + = new Loginoutclickhandler (Loginoutcontrol1_loginoutclick);

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)
{
Registering User Control Events
This. Loginoutcontrol1.loginoutclick + = new Loginoutclickhandler (Loginoutcontrol1_loginoutclick);
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e);
}
private void InitializeComponent ()
{
This. Dropdownlist1.selectedindexchanged + = new System.EventHandler (this. dropdownlist1_selectedindexchanged);
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
private void Loginoutcontrol1_loginoutclick (object sender, Loginouteventargs e)
{
Switch (e.type)
{
Case Loginclicktype.longin:
This. Labelmsg.text = "You clicked the login button, the operation result:" +e.result.tostring ();
Break
Case Loginclicktype.longout:
This. Labelmsg.text = "You clicked the Logout button, the operation result:" +e.result.tostring ();
Break
}
}
private void DropDownList1_SelectedIndexChanged (object sender, System.EventArgs e)
{
This. LOGINOUTCONTROL1.LG = (Language) this. Dropdownlist1.selectedindex;
This. Loginoutcontrol1.changelanguage ((Language) this. Dropdownlist1.selectedindex);
}
}
}

When the user changes the language of the control by selecting the Drop-down list in the foreground, this is done through the LG attribute, but there is also a method changelanguage that can achieve the same function. In addition, the Loginoutclick event is triggered by clicking the login or Logout button to assign a value to the Labelmsg.text property in the page to get the result of the operation.
Summary, user control for programmers to bring a very high development efficiency and reusability, but also in the performance of a great improvement, formerly known as asp+, in fact, I think ASP.net and ASP have no direct contact. And I want to be a friend of the application like me in the development of Web programs prefer to use code separation, so that the structure clearer, and modify and manage. Compared with the ASP program, he is compiled, the introduction of object-oriented design ideas, but also inevitably brought about his complexity, to develop a high standard of asp.net procedures for the design of the model, hierarchical structure of the division, here is still more exquisite. In short, he is more like a Windows Forms program than writing a VB script.

trackback:http://tb.blog.csdn.net/trackback.aspx?postid=22026

[Click here to collect this article] published on April 02, 2004 3:37 PM


Pplunlce published in 2004-11-07 4:53 PM ip:218.76.141.*
Landlord Hello!
I would like to ask the landlord, "another file defines the enumeration and parameter classes:" refers to which file???


Autumn Maple published in 2004-11-08 9:33 AM ip:211.140.56.*
Using System;
Namespace ZZ
{
public class Loginouteventargs:eventargs
{
Private Loginclicktype type;
private bool result;

Public Loginouteventargs (Loginclicktype type,bool result): Base ()
{
This.type = type;
This.result = result;
}
Public Loginclicktype Type
{
Get{return This.type;}
}
Operation Results,
public bool Result
{
Get{return This.result;}
}
}
Type of action
public enum Loginclicktype:int
{
Longin,
Longout
}
Definition language
public enum Language
{
Chinese,
中文版
}
}


Pplunlce published in 2004-11-10 7:37 PM ip:218.76.141.*
So how to relate to LogInOutControl.ascx.cs, always have a name?
Thank you, sir!


Pplunlce published in 2004-11-10 7:38 PM ip:218.76.141.*
So how did it relate to LogInOutControl.ascx.cs? Do you always have a name?



Lee published in 2004-12-31 1:05 AM ip:61.150.12.*
Thank you landlord's method, writing components seems to be the road to learn, please help us a lot of beginners,


Autumn Maple published in 2005-03-17 9:23 AM IP:
Trackback from "The source code for some previous articles":

Ping back from: blog.csdn.net


Alang published in 2005-04-26 8:43 PM ip:218.80.200.*
Landlord Hello, I want to know how to write a asp.net server control table mouse
Click a cell's custom event,!!!!!!!!!
Thanks for the first.



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.