Asp.net開發(fā)要求,就是用戶控件(UserControl)是集成一個(gè)功能,需要處理好的數(shù)據(jù),然后存數(shù)據(jù)庫中并顯示于網(wǎng)頁上,讓用戶能檢測到處理的數(shù)據(jù)情況。
本演示中,Insus.NET只是作了一個(gè)簡單的舉例而已,并應(yīng)用了委托來實(shí)現(xiàn)。為了讓大家更能理解,可以先看看操作動(dòng)畫演示:
在你新建一站點(diǎn)上,創(chuàng)建一個(gè)PageB.aspx網(wǎng)頁以及UcB.ascx用戶控件,在UcB.ascx上拉一個(gè)TextBox和CheckBox以及Button控件:
復(fù)制代碼 代碼如下:
UcB.ascx
%@ Control Language="C#" AutoEventWireup="true" CodeFile="UcB.ascx.cs" Inherits="UcB" %>
fieldset>
legend>User control field/legend>
Catalog:
asp:TextBox ID="TextBox1" runat="server">/asp:TextBox>br />
Enabled:
asp:CheckBox ID="CheckBox1" runat="server" />br />
asp:Button ID="Button1" runat="server" Text="Process..." OnClick="Button1_Click" />
/fieldset>
在UcB.ascx.cs代碼頁中,寫一個(gè)委托屬性,將用來存儲(chǔ)所有用戶控件中處理的數(shù)據(jù)。
復(fù)制代碼 代碼如下:
View Code
private Delegate _InsusData;
public Delegate InsusData
{
set
{
_InsusData = value;
}
}
然后在Button的OnClick事件中,把數(shù)據(jù)存入剛才寫好的屬性:
復(fù)制代碼 代碼如下:
View Code
protected void Button1_Click(object sender, EventArgs e)
{
object[] data = new object[2];
data[0] = this.TextBox1.Text.Trim ();
data[1] = this.CheckBox1.Checked;
_InsusData.DynamicInvoke(data);
}
完整的UcB.ascx.cs代碼:
復(fù)制代碼 代碼如下:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UcB : System.Web.UI.UserControl
{
private Delegate _InsusData;
public Delegate InsusData
{
set
{
_InsusData = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
object[] data = new object[2];
data[0] = this.TextBox1.Text.Trim ();
data[1] = this.CheckBox1.Checked;
_InsusData.DynamicInvoke(data);
}
}
到這里,我需要把用戶控件UcB拉到網(wǎng)頁P(yáng)ageB中,并放置好相應(yīng)的控件來顯示數(shù)據(jù):
復(fù)制代碼 代碼如下:
PageB.aspx
%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageB.aspx.cs" Inherits="PageB" %>
%@ Register Src="UcB.ascx" TagName="UcB" TagPrefix="uc1" %>
!DOCTYPE html>
html xmlns="http://www.w3.org/1999/xhtml">
head runat="server">
title>/title>
/head>
body>
form id="form1" runat="server">
uc1:UcB ID="UcB1" runat="server" />
br />
fieldset>
legend>Page Content
/legend>
Show User Control Data:br />
Catalog:
asp:Label ID="Label1" runat="server" Text="">/asp:Label>
br />
Enabled:
asp:CheckBox ID="CheckBox1" runat="server" Enabled="false" />br />
/fieldset>
/form>
/body>
/html>
在PageB.aspx.cs代碼內(nèi),宣告一個(gè)委托方法,并帶兩個(gè)參數(shù):
復(fù)制代碼 代碼如下:
private delegate void GetUserControlData(string catalog, bool isEnable);
再寫一個(gè)與剛才寫好委托方法(相同的參數(shù))的方法,用參數(shù)為頁面的控件顯示數(shù)據(jù):
復(fù)制代碼 代碼如下:
void ShowDataToPage(string catalog, bool isEnable)
{
this.Label1.Text = catalog;
this.CheckBox1.Checked = isEnable;
}
最后是在PageB.aspx.cs內(nèi)的Page_Load事件中,去實(shí)現(xiàn)獲取用戶控件的數(shù)據(jù):
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
GetUserControlData obj = new GetUserControlData(ShowDataToPage);
this.UcB1.InsusData = obj;
}
OK,完整的PageB.aspx.cs代碼:
復(fù)制代碼 代碼如下:
PageB.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class PageB : System.Web.UI.Page
{
private delegate void GetUserControlData(string catalog, bool isEnable);
protected void Page_Load(object sender, EventArgs e)
{
GetUserControlData obj = new GetUserControlData(ShowDataToPage);
this.UcB1.InsusData = obj;
}
void ShowDataToPage(string catalog, bool isEnable)
{
this.Label1.Text = catalog;
this.CheckBox1.Checked = isEnable;
}
}
您可能感興趣的文章:- asp.net頁面master頁面與ascx用戶控件傳值的問題
- asp.net 動(dòng)態(tài)添加多個(gè)用戶控件
- asp.net 用戶控件讀取以及賦值
- asp.net 用戶控件中圖片及樣式問題
- asp.net動(dòng)態(tài)加載用戶控件,關(guān)于后臺(tái)添加、修改的思考
- ASP.NET用戶控件技術(shù)