利用委托把用户控件的值显示于网页案例应用
程序员文章站
2024-03-03 23:10:52
asp.net开发要求,就是用户控件(usercontrol)是集成一个功能,需要处理好的数据,然后存数据库中并显示于网页上,让用户能检测到处理的数据情况。 本演示中,in...
asp.net开发要求,就是用户控件(usercontrol)是集成一个功能,需要处理好的数据,然后存数据库中并显示于网页上,让用户能检测到处理的数据情况。
本演示中,insus.net只是作了一个简单的举例而已,并应用了委托来实现。为了让大家更能理解,可以先看看操作动画演示:
在你新建一站点上,创建一个pageb.aspx网页以及ucb.ascx用户控件,在ucb.ascx上拉一个textbox和checkbox以及button控件:
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代码页中,写一个委托属性,将用来存储所有用户控件中处理的数据。
view code
private delegate _insusdata;
public delegate insusdata
{
set
{
_insusdata = value;
}
}
然后在button的onclick事件中,把数据存入刚才写好的属性:
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代码:
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拉到网页pageb中,并放置好相应的控件来显示数据:
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代码内,宣告一个委托方法,并带两个参数:
private delegate void getusercontroldata(string catalog, bool isenable);
再写一个与刚才写好委托方法(相同的参数)的方法,用参数为页面的控件显示数据:
void showdatatopage(string catalog, bool isenable)
{
this.label1.text = catalog;
this.checkbox1.checked = isenable;
}
最后是在pageb.aspx.cs内的page_load事件中,去实现获取用户控件的数据:
protected void page_load(object sender, eventargs e)
{
getusercontroldata obj = new getusercontroldata(showdatatopage);
this.ucb1.insusdata = obj;
}
ok,完整的pageb.aspx.cs代码:
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;
}
}
本演示中,insus.net只是作了一个简单的举例而已,并应用了委托来实现。为了让大家更能理解,可以先看看操作动画演示:
在你新建一站点上,创建一个pageb.aspx网页以及ucb.ascx用户控件,在ucb.ascx上拉一个textbox和checkbox以及button控件:
复制代码 代码如下:
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代码页中,写一个委托属性,将用来存储所有用户控件中处理的数据。
复制代码 代码如下:
view code
private delegate _insusdata;
public delegate insusdata
{
set
{
_insusdata = value;
}
}
然后在button的onclick事件中,把数据存入刚才写好的属性:
复制代码 代码如下:
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代码:
复制代码 代码如下:
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拉到网页pageb中,并放置好相应的控件来显示数据:
复制代码 代码如下:
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代码内,宣告一个委托方法,并带两个参数:
复制代码 代码如下:
private delegate void getusercontroldata(string catalog, bool isenable);
再写一个与刚才写好委托方法(相同的参数)的方法,用参数为页面的控件显示数据:
复制代码 代码如下:
void showdatatopage(string catalog, bool isenable)
{
this.label1.text = catalog;
this.checkbox1.checked = isenable;
}
最后是在pageb.aspx.cs内的page_load事件中,去实现获取用户控件的数据:
复制代码 代码如下:
protected void page_load(object sender, eventargs e)
{
getusercontroldata obj = new getusercontroldata(showdatatopage);
this.ucb1.insusdata = obj;
}
ok,完整的pageb.aspx.cs代码:
复制代码 代码如下:
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;
}
}
推荐阅读