Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > C#/.NET入门教程

用户控件(ASCX)向网页(ASPX)传值使用反射实现

来源:中文源码网    浏览:291 次    日期:2024-04-29 04:39:22
【下载文档:  用户控件(ASCX)向网页(ASPX)传值使用反射实现.txt 】


用户控件(ASCX)向网页(ASPX)传值使用反射实现
用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现。在站点中,建一个网页以及一个用户控件。 网页切换至设计模式,拉用户控件至网页上。 Default.aspx: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Src="InsusUC.ascx" TagName="InsusUC" TagPrefix="uc1" %>


Hi, You input infor as below:
first textbox value:
Second textbox value:
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
Last Name
写铵钮事件,首先引用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 }); } } 演示:

相关内容