The user control passes a value to the Web page, which is very much, and this blog attempts to use reflection to implement it. In the site, build a Web page and a user control. The Web page switches to design mode, pulling the user control to the Web page.
Default.aspx:
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= " Default.aspx.cs "inherits=" _default%>
<%@ Register src= "Insusuc.ascx" tagname= "Insusuc" tagprefix= "uc1"%& Gt
<! DOCTYPE html>
<title ></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<uc1:insusuc id= "InsusUC1" runat= "server"/>
<br/>
<br/>
Hi, you input infor as Below:<br/>
A-textbox value:
<asp:label id= "Labelshowfirstvalue" runat= "Server" text= "Forecol" Or= "Red" ></asp:label><br/>
Second textbox value:
<asp:label id= "Labelshowlastvalue" runat= "Server" text= "" forecolor= "Red" ></asp:Label>
</div>
</form>
</body>
& Lt;/html>
Default.aspx.cs, build a public method with two parameters.
Copy Code code as follows:
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;
}
}
Next, we create a user control:
Copy Code code as follows:
<%@ control language= "C #" autoeventwireup= "true" codefile= "InsusUC.ascx.cs" inherits= "Insusuc"%>
The 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"/>
Write the Ammonium button event, first referencing the namespace using System.Reflection;
About type. InvokeMember () method, you can refer to Msdn:http://msdn.microsoft.com/zh-cn/library/de3dhzwy (v=vs.80). aspx
Copy Code code as follows:
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});
}
}
Demonstrate: