用户控件(ASCX)向网页(ASPX)传值使用反射实现
程序员文章站
2024-03-04 12:00:17
用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现。在站点中,建一个网页以及一个用户控件。 网页切换至设计模式,拉用户控件至网页上。 default.aspx:...
用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现。在站点中,建一个网页以及一个用户控件。 网页切换至设计模式,拉用户控件至网页上。
default.aspx:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<%@ register src="insusuc.ascx" tagname="insusuc" tagprefix="uc1" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:insusuc id="insusuc1" runat="server" />
<br />
<br />
hi, you input infor as below:<br />
first textbox value:
<asp:label id="labelshowfirstvalue" runat="server" text="" forecolor="red"></asp:label><br />
second textbox value:
<asp:label id="labelshowlastvalue" runat="server" text="" forecolor="red" ></asp:label>
</div>
</form>
</body>
</html>
default.aspx.cs,建一个带两个参数的public方法。
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
public void readucmessage(string value1, string value2)
{
this.labelshowfirstvalue.text = value1;
this.labelshowlastvalue.text = value2;
}
}
接下来,我们创建一个用户控件:
<%@ control language="c#" autoeventwireup="true" codefile="insusuc.ascx.cs" inherits="insusuc" %>
first name <asp:textbox id="textboxfirstname" runat="server"></asp:textbox><br />
last name <asp:textbox id="textboxlastname" runat="server"></asp:textbox><br />
<asp:button id="buttontransmit" runat="server" text="transmit" onclick="buttontransmit_click" />
写铵钮事件,首先引用namespace using system.reflection;
有关type.invokemember()方法,可以参考msdn:http://msdn.microsoft.com/zh-cn/library/de3dhzwy(v=vs.80).aspx
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.reflection;
public partial class insusuc : system.web.ui.usercontrol
{
protected void page_load(object sender, eventargs e)
{
}
protected void buttontransmit_click(object sender, eventargs e)
{
string v1 = this.textboxfirstname.text.trim();
string v2 = this.textboxlastname.text.trim();
this.page.gettype().invokemember("readucmessage", bindingflags.invokemethod, null, this.page, new object[] { v1,v2 });
}
}
演示:
default.aspx:
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<%@ register src="insusuc.ascx" tagname="insusuc" tagprefix="uc1" %>
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:insusuc id="insusuc1" runat="server" />
<br />
<br />
hi, you input infor as below:<br />
first textbox value:
<asp:label id="labelshowfirstvalue" runat="server" text="" forecolor="red"></asp:label><br />
second textbox value:
<asp:label id="labelshowlastvalue" runat="server" text="" forecolor="red" ></asp:label>
</div>
</form>
</body>
</html>
default.aspx.cs,建一个带两个参数的public方法。
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
public void readucmessage(string value1, string value2)
{
this.labelshowfirstvalue.text = value1;
this.labelshowlastvalue.text = value2;
}
}
接下来,我们创建一个用户控件:
复制代码 代码如下:
<%@ control language="c#" autoeventwireup="true" codefile="insusuc.ascx.cs" inherits="insusuc" %>
first name <asp:textbox id="textboxfirstname" runat="server"></asp:textbox><br />
last name <asp:textbox id="textboxlastname" runat="server"></asp:textbox><br />
<asp:button id="buttontransmit" runat="server" text="transmit" onclick="buttontransmit_click" />
写铵钮事件,首先引用namespace using system.reflection;
有关type.invokemember()方法,可以参考msdn:http://msdn.microsoft.com/zh-cn/library/de3dhzwy(v=vs.80).aspx
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.reflection;
public partial class insusuc : system.web.ui.usercontrol
{
protected void page_load(object sender, eventargs e)
{
}
protected void buttontransmit_click(object sender, eventargs e)
{
string v1 = this.textboxfirstname.text.trim();
string v2 = this.textboxlastname.text.trim();
this.page.gettype().invokemember("readucmessage", bindingflags.invokemethod, null, this.page, new object[] { v1,v2 });
}
}
演示: