ASP.NET用戶控件怎么用

這篇文章主要介紹了ASP.NET用戶控件怎么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的柳城網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

ASP以及ASP.NET歷史回顧

ASP.NET技術(shù)雖然算是從ASP演變而來的,不過使用起來確實(shí)是兩碼事。我們先看看這兩個技術(shù)的歷史:在90年代初,Microsoft為Web程序員提供的 Active Server Pages(ASP)革命性地改變了Web的編程。它可以利用十分易用的模型在Web服務(wù)器上動態(tài)生成HTML,并且很容易的實(shí)現(xiàn)了對數(shù)據(jù)庫的訪問,就當(dāng)時(shí)來說,這是一項(xiàng)多么吸引人的技術(shù),包括現(xiàn)在Internet上的許多web站點(diǎn)都是用Asp寫的,我的同事前輩們更是玩Asp的高手,經(jīng)歷這么多年而不衰,可見他的成功。

但是,技術(shù)是在不斷的發(fā)展著,引用某位Net專家的話講――如今Web編程的狀態(tài)還是落后的。因此Microsoft提出了第二代編程模型――Web窗體。Web窗體模型作為Asp.net的一部分,而Asp.net又是.Net框架的一個部分。他的編程模型是基于事件的,使用他更像是在進(jìn)行Windows窗體編程,這一點(diǎn)也正是我決定去學(xué)習(xí)使用他的一個重要原因,也胡亂看了一些這方面的書,寫這篇文章的目的也就是和各位Asp.net初學(xué)者和還沒有為用戶控件添加過自定義事件的同行分享一下經(jīng)驗(yàn)。

建立一個簡單的ASP.NET用戶控件

廢話少說,下面就讓我們先建立一個用戶控件吧,這里就用一個簡單登錄用戶控件來做演示。

先來看看用戶控件的前臺代碼(LogInOutControl.ascx文件):

< %@ 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">用戶:< /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">密碼:< /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="登錄" runat="server">< /asp:Button>      < asp:Button id="ButtonLogOut" Width="49px" Text="注銷" runat="server">< /asp:Button>< /FONT>< /TD> < /TR> < /TABLE>

我們簡單的放了兩個Label,兩個TextBox,兩個Button以及一個Html表。

接下去就是為LogInOutControl.ascx.cs文件添加代碼了。

首先定義一個delegate,其中LogInOutEventArgs類是從EventArgs類繼承,

public delegate void LogInOutClickHandler(object sender,LogInOutEventArgs e);

我覺得把這個delegate放在LogInOutControl類外面更為合適。

接下去為控件聲明了LogInOutClick事件,如下:

public event LogInOutClickHandler LogInOutClick;

另外為了更好的使用屬性,加了Language枚舉,

private Language language;

當(dāng)然外部通過public Language Lg {get;set;}屬性來訪問。目的就是改變或者獲取當(dāng)前控件的顯示。

接下去就是定義控件事件觸發(fā)函數(shù)OnLogInOutClick,由按鈕單擊事件處理函數(shù)來完成對用戶控件事件的觸發(fā)。

ASP.NET用戶控件完整代碼如下:

namespace ZZ  {  using System;  using System.Data;  using System.Drawing;  using System.Web;  using System.Web.UI.WebControls;  using System.Web.UI.HtmlControls;   // 定義代理  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;     //方法     public void ChangeLanguage(Language language)     {      this.Lg = language;     }     //屬性     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 = "用戶:";         this.LabelPassword.Text ="密碼:";         this.ButtonLogIn.Text = "登錄";         this.ButtonLogOut.Text = "注銷";        }       }      }     }     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);     }     #region Web 窗體設(shè)計(jì)器生成的代碼     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)     {      //注銷代碼省略      OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongOut,true));     }     //驗(yàn)證函數(shù)     private bool CustomValidate(string userName,string password)     {      //驗(yàn)證代碼省略,假設(shè)通過      return true;     }  }  }

另外一個文件定義了枚舉和參數(shù)類:

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;}     }     //操作結(jié)果,     public bool Result     {      get{return this.result;}     }  }  //操作類型  public enum LogInClickType : int {     LongIn,     LongOut  }  //定義語言  public enum Language   {     Chinese,     English  }  }

接下去看看在aspx頁面里面使用ASP.NET用戶控件。

新建一個Default.aspx頁面,拖一個LogInOutControl用戶控件到上面。

< %@ 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">中文< /asp:ListItem>       < asp:ListItem Value="1">英文< /asp:ListItem>      < /asp:DropDownList>< /FONT>    < /form> < /body> < /HTML>

在后臺代碼中添加事件和屬性。

雖然在前臺添加了LogInOutControl1,但是后臺代碼中不會生成protected LogInOutControl LogInOutControl1;這條語句,我覺得很奇怪,不管先加上他。

接著在Page_Load事件中注冊LogInOutClick事件:

this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick);

完整代碼如下:

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)     {      //注冊用戶控件事件      this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick);     }     #region Web 窗體設(shè)計(jì)器生成的代碼     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 = "你點(diǎn)擊了登錄按鈕,操作結(jié)果:"+e.Result.ToString();        break;       case LogInClickType.LongOut:        this.LabelMsg.Text = "你點(diǎn)擊了注銷按鈕,操作結(jié)果:"+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);     }  }  }

當(dāng)用戶在前臺通過選擇下拉框列表來改變控件的語言,這里通過Lg屬性來完成,不過這里也加了一個方法ChangeLanguage也可以實(shí)現(xiàn)同樣的功能。另外,通過點(diǎn)擊登陸或注銷按鈕觸發(fā)LogInOutClick事件來給頁面中的LabelMsg.Text屬性賦值從而得到操作結(jié)果。

總結(jié),用戶控件為程序員帶來了很高的開發(fā)效率和重用性,更是在性能方面有了很大的提高,以前稱為Asp+,其實(shí)我認(rèn)為Asp.net跟Asp沒有什么直接聯(lián)系。而且我想做應(yīng)用程序的朋友和我一樣在開發(fā)Web程序時(shí)更喜歡采用代碼分離方式,這樣結(jié)構(gòu)更清晰,便與修改和管理。同Asp程序相比,他是編譯型的,引入了面向?qū)ο蟮脑O(shè)計(jì)思想,也就不可避免的帶來了他的復(fù)雜性,要想開發(fā)高水準(zhǔn)的Asp.net程序,對于模式的設(shè)計(jì),層次結(jié)構(gòu)的劃分,這里還是比較講究的??傊?,他更像是在編Windows窗體程序,而不是在寫VB腳本。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“ASP.NET用戶控件怎么用”這篇文章對大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)頁題目:ASP.NET用戶控件怎么用
網(wǎng)頁地址:http://bm7419.com/article48/phddep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊電子商務(wù)、云服務(wù)器微信公眾號、用戶體驗(yàn)、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)