In asp.net 2.0, to call each other between different web controls, you must <% @ reference virtualpath = "another control name">. For example:
default.aspx: <form id="form1" runat="server"> <uc1:webusercontrol id="webusercontrol1" runat="server"> </uc1:webusercontrol> <uc2:webusercontrol2 id="webusercontrol2_1" runat="server" /> </form>
|
To achieve this, after pressing the button of Control 1, the specified text will be displayed in the text box of Control 2.
On the homepage, control 1 and Control 2 are called respectively:
webcontrol.ascx: <%@ control language="c#" autoeventwireup="true" codefile="webusercontrol.ascx.cs" inherits="webusercontrol" %> <%@ reference virtualpath="~/webusercontrol2.ascx" %> <asp:button id="button1" runat="server" onclick="button1_click" text="button" />
|
Place a button here and use reference to reference Control 2:
webcontrol.ascx.cs: protected void button1_click(object sender, eventargs e) { webusercontrol2 w = page.findcontrol("webusercontrol2_1") as webusercontrol2; w.text = "hello all!"; }
|
For control 2:
<%@ control language="c#" autoeventwireup="true" codefile="webusercontrol2.ascx.cs" inherits="webusercontrol2" %> <asp:textbox id="textbox1" runat="server"></asp:textbox>
|
Codebehind code of Control 2:
public partial class webusercontrol2 : system.web.ui.usercontrol { protected void page_load(object sender, eventargs e) { } public string text { set { textbox1.text = value; } } }
|