一、母版頁
在制作頁面的過程中, 多個頁面往往具有相同的頁面Header和頁面Footer, 多個頁面只是在中間部分有變化. 那么我們完全可以避免在每個頁面中都寫一遍頁頭和頁尾的代碼, 這種技術(shù)就是母版頁, 其實用戶控件也可以實現(xiàn)相同的功能. 我們可以使用母版頁把頁面中相似的部分提取出來, 而把各面之間不同的部分放在一個稱為ContentPlaceHolder的標(biāo)簽對中, 之后我們創(chuàng)建頁面的時候, 可以勾選”選擇母版頁”, 進而創(chuàng)建那些框架類似的頁面, 這時即使我們不寫一行代碼, 剛創(chuàng)建的頁面也包含有母版頁的內(nèi)容. 剩下的工作, 就是在新建頁面的Content標(biāo)簽中寫內(nèi)容就可以了, 注意: 母版頁中是可以規(guī)定多個ContentPlaceHolder塊的. 母版頁及頁面的搭建都比較簡單, 這里不做說明.
關(guān)于母版頁的兩個小技巧:
1. 修改內(nèi)容也的標(biāo)題: 只需要在內(nèi)容也的頁面指令處, 添加屬性Title = “Page Title”即可, 這樣Page Title就會像是在IE的標(biāo)題欄.
2. 內(nèi)容頁中修改母版頁中的數(shù)據(jù):
a.) 在母版頁中添加控件(除ContentPlaceHolder標(biāo)簽對外的任何位置). 常用來放置登錄后的用戶信息.
b.) 在母版頁的后臺cs文件中, 制作公有屬性將剛添加的控件暴露出來.
c.) 在內(nèi)容頁中添加個頁面指令, 該頁面指令的作用, 是使內(nèi)容頁識別我們在母版頁中添加的自定義信息. 指令如下:
<%@ MasterType VirtualPath=“~/MagerPage.master” %>
d.) 在內(nèi)容頁中用母版頁對象this.Master獲取母版頁中添加的控件的引用(通過自定的屬性來獲取), 進而達到通過內(nèi)容頁操作母版頁的目的.
二、用戶控件(Web User Control)
用戶控件是微軟在ASP.NET中定義的一個類, 在VS編輯器模板中的名稱是Web User Control, 后綴名為ascx. 用戶控件和母版頁的功能類似(在沒有母版頁這項技術(shù)的時候, 類似頁面Header和頁面Footer的工作就是通過用戶控件實現(xiàn)的).
用戶控件(Web User Control)和自定義控件(第三方控件, Custom Control)區(qū)別:
容易引起混淆的地方是用戶控件和自定義控件(第三方控件), 這兩者最根本的區(qū)別是: 用戶控件是微軟定義的類, ASP.NET甚至為我們定義好了模板, 用戶控件的作用類似于C#中的常量, 我們將頁面上多次用到的重復(fù)部分做成用戶控件, 但自定義控件是我們程序員自己定義的, 我們通過定義一個類繼承自某個控件類, 來完善控件的功能或?qū)⒍鄠€控件組成復(fù)合控件. 其他的區(qū)別還有: 用戶控件只能放在解決方案管理器中, 我們在解決方案管理器的項目中拖動用戶控件到頁面將會自動生成事先定義的重復(fù)代碼, 而自定義控件是放在工具箱中的, 與TextBox、Button等控件屬于同一級別.
前者解決了重復(fù)代碼的問題(生成內(nèi)容相對固定), 后者則創(chuàng)建了一個新的控件(可根據(jù)參數(shù)調(diào)整). 就好比: 建筑行業(yè)中, 用用戶控件就類似于磚頭和水泥組合成的墻, 在我的建筑工程中, 我可以說: “這兒來一堵墻, 那兒再來一堵墻”來重復(fù)磚頭和水泥的組合; 自定義控件就類似于建好的某個房間, 是個成熟的東西. 我可以說:”這兒來個房間, 房間大小是15*15米, 形狀是橢圓的”等等.
創(chuàng)建用戶控件:
用戶控件的制作相對簡單, 你完全可以用寫頁面的方式來寫用戶控件. 當(dāng)我們用VS編輯器在網(wǎng)站項目中創(chuàng)建一個擴展名為.ascx的用戶控件時, 可以看到用戶控件派生子System.Web.UI.UserControl, 注意: 拖動用戶控件時, 只能在設(shè)計視圖中. 當(dāng)拖放操作完成后, 會在頁面中生成<%@ Register …%>指令, 其中src表示用戶控件路徑; tagname是用戶控件的類名, 同時也是相應(yīng)標(biāo)簽的名字, 如: <ucl: tagname…>; tagprefix表示控件的前綴, 如: ucl. 注意工具箱中的控件前綴為asp.
1.) 初始化用戶控件: 初始化用戶控件的工作不要放在構(gòu)造函數(shù)中, 最好放在頁面對象的Page_Init事件中.
2.) 可以再用戶控件中定義屬性、方法、事件(經(jīng)常將用戶控件中的顯示控件以公有屬性的形式暴露出來, 在前臺頁面使用用戶控件的id獲得公有屬性).
3.) 可以再用戶控件上提供局部緩存管理.
通過如下指令, 設(shè)置用戶控件的緩存: 在用戶控件的文件(*.ascx)中, 添加頁面指令:
<%@ OutputCache Duration=”10000” VaryByParam=”page;categoryID” %>
其中, duration表示以秒為單位的緩存時間; VaryByParam表示在 請求參數(shù) 中的page和categoryID發(fā)生變化時需要單獨的緩存處理, 也就是說當(dāng)頁面的請求參數(shù)的值發(fā)生變化時, 瀏覽器會自動發(fā)送一個新的請求并重新緩存頁面. 注意: <OutputCache指令中VaryByParam屬性的值以分號分隔, 而相應(yīng)頁面的URL中就是用&拼接的名值對.
Location屬性用來設(shè)置緩存的位置, 可以再本地緩存也可以在服務(wù)器中緩存, 但是由于用戶控件就相當(dāng)于部分頁面, 因此緩存在本地沒有意義, 用戶控件或部分頁面的緩存通常放在服務(wù)器上.
再*.ascx.cs中使用this.CachePolicy.Dependency 設(shè)置緩存依賴.
三、自定義控件(第三方控件)
控件是ASP.NET技術(shù)的核心, 微軟在ASP.NET中針對常見的Web應(yīng)用開發(fā)了很多基本控件, 開發(fā)人員可以使用這些控件完成模板頁的制作(*.aspx頁面). 但是在實際的開發(fā)過程中, 可能有些功能無法通過這些基本控件實現(xiàn), 也有可能需要對基本控件做些擴充, 這時候就需要用到自定義控件(Custom Control), 也有公司喜歡稱為第三方控件.
自定義控件和label、textbox控件一樣, 它就是一個控件, 需要放在”工具箱(ToolBox)”中. 前面說過, 與用戶控件不同, 用戶控件是用來將項目中多次用到的代碼封裝起來, 當(dāng)我們拖放一個用戶控件時, 機器將為我們生成那些重復(fù)的代碼; 而自定義控件將會有自己的功能、屬性及表現(xiàn)形式.
創(chuàng)建自定義控件:
創(chuàng)建自定義控件有三種方式: \
第一種是從Control類派生, 如果自定義控件不需要在網(wǎng)頁上生成內(nèi)容, 僅在服務(wù)器端參與請求處理過程, 就可以從Control類派生.
第二種是從WebControl類派生, 如果自定義控件需要在網(wǎng)頁上顯示出來, 就可以從WebControl類派生.
第三種是從CompositeControl類派生, 這是工作中比較常用的情況. 該種情況下, 自定義控件不是用Control和WebControl直接派生, 而是先找到功能相近的ASP.NET控件或某個復(fù)合控件, 然后自定義控件派生自找到的控件, 并在其基礎(chǔ)上做些擴展.
自定義控件就是一個類, 在開發(fā)過程中表現(xiàn)為類庫項目. 其中, Windows Form窗體程序中的自定義控件在VS08中稱為CustomControl(派生自Control類), 而在Web Form程序中, 自定義控件稱為Server Control(派生自WebControl類), 他們派生自不同的類. 參見下圖, 展示了自定義控件所涉及的相關(guān)內(nèi)容:

在實際的開發(fā)過程中, 前兩種的從無到有的創(chuàng)建控件的方式, 用的很少, 其中最主要的是重寫Render方法. 這些常用的控件微軟都已經(jīng)為我們做好了實現(xiàn), 我們更常見的方式就是從CompositeControl類派生, 使用多個基本控件組合出一個復(fù)合控件來完成復(fù)雜的功能.
我們通過示例來說明自定義控件的制作過程, 自定義登錄控件是面試??嫉?/SPAN>:
注意: Asp.Net提供了一個名稱為Login的登錄控件, 為了避免可能的混亂, 在我們創(chuàng)建WebForm或自定義控件時, 避免使用Login做名稱.
制作自定義控件的過程:
1. 自定義類派生自System.Web.UI.WebControls.CompositeControl, 并在類中創(chuàng)建自定義控件所需要的基本控件類型的變量, 而且需要對用來顯示內(nèi)容的控件 或者針對顯示內(nèi)容控件的 有關(guān)顯示的屬性 而制作公有屬性.
注意: 在制作屬性的過程中, 需要在Get和Set訪問器中使用EnsureChildControls()方法強制繪制基本控件.
2. 必須在自定義控件的類中重寫CreateChildControl()方法, 該方法負責(zé)創(chuàng)建基本控件(或者說子控件), 并對其進行初始化設(shè)置, 相當(dāng)于自定義控件類中的構(gòu)造函數(shù). 最后, 別忘了把初始化完畢的基本控件通過this.Controls.Add()方法添加到當(dāng)前的自定義控件中.
3. 必須在自定義控件的類中重寫RenderContents()方法, 該方法用來布局. 可以使用基本控件上的RenderControl()方法繪制基本控件. 注意: 因為在該方法中要生成內(nèi)容, 所以應(yīng)該先設(shè)置布局元素的屬性(Attribute), 后寫生成的標(biāo)簽.
4. 必須在自定義控件的類中重寫一個布局的屬性(override HtmlTextWriterTag TagKey). 該屬性用于設(shè)置布局的方式, table還是div.
5. 添加到工具箱中, 方法: 右鍵工具箱, 選擇”選擇項”, . 順便提下, 我們可以為自定義控件添加強名稱后放入GAC中, 這樣每次只需要拖動即可.
6. 為自定義控件添加事件: 在自定義控件的類中, 直接定義事件即可(用EventHandler委托), 定義好事件后, 需要在按鈕的Click事件中注冊事件處理方法. 為了能夠?qū)崿F(xiàn)雙擊自定義控件, 自動在后臺.cs文件中添加事件處理器模板, 自定義控件的類中的按鈕的事件處理器, 需要先判斷事件不為空, 然后通過定義的事件名調(diào)用方法即可. 注意: 關(guān)于事件名方法的參數(shù), 如果希望點擊整個自定義控件執(zhí)行某個事件處理器, 那么事件名方法的參數(shù)為this和e, this表示的是自定義控件; 如果希望點擊自定義控件中的某個按鈕后激發(fā)某個事件處理器, 那么事件名方法的參數(shù)為sender和e, sender表示點擊的按鈕. 一般情況下, 用this作參數(shù)時, 在后臺.cs文件中, 可以通過sender取得自定義控件的所有屬性, 因此可以將自定義控件的主要事件用個this作參數(shù), 而用sender作參數(shù)時, 在后臺.cs文件中, 可以通過自定義控件的ID獲得自定義控件的所有屬性.
備注:
1. 可以給自定義控件添加圖標(biāo), 做法很簡單, 只要在自定義控件的類的前面加上標(biāo)簽:
[System.Drawing.ToolboxBitmap(typeof(自定義控件名), "命名空間.圖片名(與類名相同).bmp")] 即可.
設(shè)置圖標(biāo)有3個注意事項: a. 一定要是16*16的bmp圖片 b. 圖片名稱一定要與類名相同且需要加命名控件 c.右鍵點擊圖片, 在BuildAction中選中”嵌入資源”.
2. 自定義控件視圖狀態(tài)的管理(對于那些本身不是控件類或沒有派生自Controls的類), 需要實現(xiàn)System.Web.UI.IStateManager接口. 之后再使用控件的cs文件中, 通過重寫SaveViewState()和LoadViewState()這兩個方法, 實現(xiàn)自定義控件的視圖狀態(tài)管理.
//示例代碼: 一共4頁面, 包括母版頁、default.aspx(自定義控件)、Register(用戶控件)、UserManagement(母版頁)
//MasterPage.master
 代碼
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www./1999/xhtml"> <head runat="server"> <title>MasterPage</title> </head> <body style="text-align:center;"> <form id="frm_master" runat="server" style="width:800px;"> <div id="header"> <table> <tr><td><a href="Default.aspx">首頁(自定義控件或第三方控件)</a> </td><td><a href="Register.aspx">注冊用戶(用戶控件)</a> </td><td><a href="UserManagement.aspx">用戶管理(內(nèi)容頁修改母版頁)</a> </td></tr> </table> </div> <div> <asp:Label ID="lbl_master" runat="server" Text="母版頁的文本框信息"></asp:Label> <asp:TextBox ID="txt_master" runat="server" Text="默認為母版頁的值"></asp:TextBox><hr /><br /> <asp:ContentPlaceHolder id="cpHolder" runat="server"> </asp:ContentPlaceHolder> </div> <br style="clear:both"/><%--清除樣式--%> <div> <hr /> <br /> <table><tr><td>XXX公司</td></tr><tr><td>版權(quán)所有2010-2011</td></tr></table></div> </form> </body> </html>
//MasterPage.master.cs
 代碼
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml.Linq;
public partial class MasterPage : System.Web.UI.MasterPage { protected void Page_Load(object sender, EventArgs e) { }
//暴露母版頁中的信息, 我們只需要在內(nèi)容頁中拿到母版頁控件的引用即可, 不需要Set訪問器 public Label MasterLabel { get { return this.lbl_master; } }
public TextBox MasterTextBox { get { return this.txt_master; } } }
//default.aspx -- 自定義控件
 代碼
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %> <%@ OutputCache Duration="1" VaryByParam="count" %>
<%@ Register Assembly="CustomControl" Namespace="CustomControl" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cpHolder" Runat="Server"> <cc1:LoginControl ID="LoginControl1" runat="server" onloginclick="LoginControl1_LoginClick" onregisterclick="LoginControl1_RegisterClick" /> </asp:Content>
//default.aspx.cs
 代碼
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void LoginControl1_LoginClick(object sender, EventArgs e) { if (this.LoginControl1.UserName == "zhangsan" && this.LoginControl1.Password == "zhangsan" && this.LoginControl1.InputCode.ToUpper() == this.Session["dycode"].ToString()) { this.Response.Redirect("UserManagement.aspx"); } else { this.Page.ClientScript.RegisterStartupScript(this.GetType(), "CScript", "<script>alert('驗證失敗! ');</script>"); } } protected void LoginControl1_RegisterClick(object sender, EventArgs e) { this.Response.Redirect("Register.aspx"); }
//在控件中通過重寫基類的SavaViewState()和LoadViewState()方法, 來實現(xiàn)自定義控件的視圖狀態(tài)的管理 protected override object SaveViewState() { object x = base.SaveViewState(); object y = this.LoginControl1.SaveViewState(); return new System.Web.UI.Pair(x, y); } protected override void LoadViewState(object savedState) { if (savedState != null) { System.Web.UI.Pair pair = savedState as System.Web.UI.Pair; base.LoadViewState(pair.First); this.LoginControl1.LoadViewState(pair.Second); } } }
//dynamiccode.ashx
 代碼
<%@ WebHandler Language="C#" Class="dynamiccode" %>
using System; using System.Web;
public class dynamiccode : IHttpHandler,System.Web.SessionState.IRequiresSessionState { private static readonly string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; private Random ran = new Random(); public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/jpeg"; context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 25);//Bitmap和Metafile是抽象類System.Drawing.Image的派生類, 圖片寬度從0-79, 代表點數(shù) using(System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage(bmp)) { graphic.Clear(System.Drawing.Color.LightGreen);//類似橡皮擦, 用背靜填充 for (int i = 0; i < 20; i++) { System.Drawing.Point startpt = new System.Drawing.Point(ran.Next(0, 100), ran.Next(0, 25));//ran.Next取左不取右, //System.DrawLine需要的兩個對象 System.Drawing.Point endpt = new System.Drawing.Point(ran.Next(0, 100), ran.Next(0, 25));//System.DrawLine需要的兩個對象
//要畫直線, 需要兩個System.Drawing.Point類型的對象和1個畫筆對象 graphic.DrawLine(System.Drawing.Pens.DarkCyan, startpt, endpt); }
//畫圖片的外框 System.Drawing.Rectangle rt = new System.Drawing.Rectangle(0,0,99,24);//定義矩形框 graphic.DrawRectangle(System.Drawing.Pens.BlueViolet, rt); //畫驗證碼 string strrandom = ""; for (int i = 0; i < 5; i++) { strrandom += charset[ran.Next(0,charset.Length)]; } //保存到Session中 context.Session["dycode"] = strrandom; //定義字體 System.Drawing.Font ft = new System.Drawing.Font("黑體",20,System.Drawing.FontStyle.Bold); //定義需要被繪制的字符串的格式 System.Drawing.StringFormat sf = new System.Drawing.StringFormat(); sf.Alignment = System.Drawing.StringAlignment.Center;//垂直居中 sf.LineAlignment = System.Drawing.StringAlignment.Center;//水平居中
graphic.DrawString(strrandom, ft, System.Drawing.Brushes.DarkOliveGreen, rt, sf); }
//通過context.Response.OutputStream流, 將位圖文件寫入到瀏覽器 bmp.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); } public bool IsReusable { get { return false; } }
}
// Register.aspx -- 用戶控件
 代碼
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Register" Title="注冊用戶(用戶控件)" %>
<%@ Register src="RegisterUser.ascx" tagname="RegisterUser" tagprefix="uc1" %>
<%--需要在設(shè)計視圖下, 拖動用戶控件--%> <asp:Content ID="Cont_Register" ContentPlaceHolderID="cpHolder" Runat="Server"> <uc1:RegisterUser ID="RegisterUser1" runat="server" /> </asp:Content>
//Register.aspx.cs
 代碼
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;
public partial class Register : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //使用用戶控件中定義的公共屬性 this.RegisterUser1.RegInfo.Text = "左邊為必填項! "; } }
//RegisterUser.ascx
 代碼
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="RegisterUser.ascx.cs" Inherits="RegisterUser" %>
<div style="float:left"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td>用戶名: </td> <td style="width:150px"> <asp:TextBox ID="txt_name" runat="server"></asp:TextBox></td> <td rowspan="4"> <asp:TextBox ID="txt_infos" runat="server" BorderStyle="None" Height="100px" TextMode="MultiLine" ></asp:TextBox></td> </tr> <tr> <td>密 碼: </td> <td> <asp:TextBox ID="txt_pass" runat="server"></asp:TextBox></td> </tr> <tr> <td>重復(fù)密碼:</td> <td> <asp:TextBox ID="txt_confirm" runat="server"></asp:TextBox></td> </tr> <tr> <td>郵 箱: </td> <td> <asp:TextBox ID="txt_email" runat="server"></asp:TextBox></td> </tr> <tr> <td></td> <td> <asp:Button ID="btn_submit" runat="server" Text="注冊" onclick="btn_submit_Click" /></td> </tr> </table> </div> <div style="margin-left:50px;float:left" > <asp:Label ID="lbl_userlist" style="margin-left:50px;" runat="server" Text="已有用戶:"></asp:Label><br /> <div style="width:60%"> <asp:Repeater ID="rpt_userlist" runat="server" > <HeaderTemplate> <table cellspacing="0" cellpadding="0" border="0" style="width:100%;border-collapse:collapse"> <tr><th scope="col">用戶名</th><th scope="row">郵箱</th></tr> </HeaderTemplate> <ItemTemplate> <tr align="center"> <td><%# DataBinder.Eval(Container.DataItem,"Key") %></td> <td><%# Eval("Value") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </div><br />
//RegisterUser.ascx.cs
 代碼
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;
public partial class RegisterUser : System.Web.UI.UserControl { //使用Dictionary提供些模擬數(shù)據(jù), 供Datelist使用 System.Collections.Generic.Dictionary<string, string> users;
//初始化 protected void Page_Init(object sender, EventArgs e) { this.txt_name.Text = "用戶名只能為字母!"; users = new System.Collections.Generic.Dictionary<string, string>(); users.Add("張三", "zhangsan@zs.com"); users.Add("李四", "lisi@ls.com"); users.Add("王五", "wanwu@ww.com"); }
protected void Page_Load(object sender, EventArgs e) { BindData(); }
//定義屬性, 通常將用戶控件中的顯示控件的屬性暴露出來(用來在頁面中賦值, 只要用get獲得其引用即可) public TextBox RegInfo { get { return this.txt_infos; } }
//定義事件處理方法 protected void btn_submit_Click(object sender, EventArgs e) { string msg = string.Empty; if (string.IsNullOrEmpty(this.txt_name.Text) || string.IsNullOrEmpty(this.txt_pass.Text) || string.IsNullOrEmpty(this.txt_email.Text)) { if (string.IsNullOrEmpty(this.txt_name.Text)) { msg += "用戶名不能為空! "; msg += Environment.NewLine; } if (string.IsNullOrEmpty(this.txt_pass.Text)) { msg += "密碼不能為空! "; msg += Environment.NewLine; } if (string.IsNullOrEmpty(this.txt_email.Text)) { msg += "郵箱不能為空! "; msg += Environment.NewLine; } } else { msg = "您輸入的注冊信息如下: "; if (!string.IsNullOrEmpty(this.txt_name.Text)) { msg += this.txt_name.Text; msg += Environment.NewLine; } if (!string.IsNullOrEmpty(this.txt_pass.Text)) { msg += this.txt_pass.Text; msg += Environment.NewLine; } if (!string.IsNullOrEmpty(this.txt_email.Text)) { msg += this.txt_email.Text; msg += Environment.NewLine; } } this.txt_infos.Text = msg; }
private void BindData() { this.rpt_userlist.DataSource = this.users; this.rpt_userlist.DataBind(); } }
//UserManagerment.aspx --- 母版頁
 代碼
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="UserManagement.aspx.cs" Inherits="UserManagement" Title="用戶管理" %> <%@ MasterType VirtualPath="~/MasterPage.master" %>
<asp:Content ID="cont_usermanage" ContentPlaceHolderID="cpHolder" Runat="Server"> <label for="txt_name">用戶名</label> <asp:TextBox ID="txt_name" runat="server"></asp:TextBox> <br /> <label for="txt_tel">聯(lián)系電話</label> <asp:TextBox ID="txt_tel" runat="server"></asp:TextBox> <br /> <asp:Button ID="btn_submit" runat="server" Text="提交" onclick="btn_submit_Click" /> </asp:Content>
//UserManagerment.aspx.cs
 代碼
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq;
public partial class UserManagement : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { }
protected void btn_submit_Click(object sender, EventArgs e) { //在內(nèi)容頁中, 通過母版頁對象Master獲得已暴露的控件的引用(通過屬性獲得) this.Master.MasterLabel.Text = this.txt_name.Text; this.Master.MasterTextBox.Text = this.txt_tel.Text; } }
//自定義控件的項目, 省略了LoginControl.bmp
//LoginControl.cs
|