在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框架的一個(gè)部分。他的編程模型是基于事件的,使用他更像是在進(jìn)行windows窗體編程,這一點(diǎn)也正是我決定去學(xué)習(xí)使用他的一個(gè)重要原因,也胡亂看了一些這方面的書,寫這篇文章的目的也就是和各位asp.net初學(xué)者和還沒有為用戶控件添加過自定義事件的同行分享一下經(jīng)驗(yàn)。
廢話少說,下面就讓我們先建立一個(gè)用戶控件吧,這里就用一個(gè)簡單登錄用戶控件來做演示。
先來看看用戶控件的前臺代碼(loginoutcontrol.ascx文件):
<%@ control language="c#" autoeventwireup="false" codebehind="loginoutcontrol.ascx.cs" inherits="zz.loginoutcontrol" targetschema="http://schemas.microsoft.com/intellisense/ie5"%>
<table id="table1" 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>
我們簡單的放了兩個(gè)label,兩個(gè)textbox,兩個(gè)button以及一個(gè)html表。
接下去就是為loginoutcontrol.ascx.cs文件添加代碼了。
首先定義一個(gè)delegate,其中l(wèi)oginouteventargs類是從eventargs類繼承,
public delegate void loginoutclickhandler(object sender,loginouteventargs e);
我覺得把這個(gè)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ā)。
完整代碼如下:
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;
}
}
}
另外一個(gè)文件定義了枚舉和參數(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頁面里面使用。
新建一個(gè)default.aspx頁面,拖一個(gè)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屬性來完成,不過這里也加了一個(gè)方法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腳本。
trackback: http://tb.blog.csdn.net/trackback.aspx?postid=22026
[點(diǎn)擊此處收藏本文] 發(fā)表于 2004年04月02日 3:37 pm
pplunlce 發(fā)表于2004-11-07 4:53 pm ip: 218.76.141.*
樓主你好!
請問樓主,"另外一個(gè)文件定義了枚舉和參數(shù)類:"是指哪個(gè)文件了???
秋楓 發(fā)表于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;}
}
//操作結(jié)果,
public bool result
{
get{return this.result;}
}
}
//操作類型
public enum loginclicktype : int
{
longin,
longout
}
//定義語言
public enum language
{
chinese,
english
}
}
pplunlce 發(fā)表于2004-11-10 7:37 pm ip: 218.76.141.*
那怎么樣與loginoutcontrol.ascx.cs關(guān)聯(lián)起來,總有個(gè)名字吧?
謝謝了!
pplunlce 發(fā)表于2004-11-10 7:38 pm ip: 218.76.141.*
那怎么樣與loginoutcontrol.ascx.cs關(guān)聯(lián)起來了?總也有個(gè)名字嗎?
lee 發(fā)表于2004-12-31 1:05 am ip: 61.150.12.*
謝謝樓主的方法,寫組件看來是以后必學(xué)之路,請樓主多多幫我們這些初學(xué)者,thanks
秋楓 發(fā)表于2005-03-17 9:23 am ip:
trackback來自《對以前的一些文章提供程序源代碼》:
ping back來自:blog.csdn.net
阿郎 發(fā)表于2005-04-26 8:43 pm ip: 218.80.200.*
樓主你好,我想知道怎么樣寫一個(gè)asp.net服務(wù)器控件table的鼠標(biāo)
單擊某個(gè)單元格的自定義事件,急!!!!!!!!!
先謝了。
新聞熱點(diǎn)
疑難解答
圖片精選