打印

[asp.net教程] asp.net 2.0中不同web控件之间的相互调用

asp.net 2.0中不同web控件之间的相互调用

在asp.net 2.0中,要在不同的web控件之间互相调用,必须要<%@ reference virtualpath="另一控件名称“>
  来引用,举例如下
   
   
  default.aspx:
  <form id="form1" runat="server">
   <uc1:webusercontrol id="webusercontrol1" runat="server">
   </uc1:webusercontrol>
   <uc2:webusercontrol2 id="webusercontrol2_1" runat="server" />
   </form>
   
  我们要实现的是,按下控件1的按钮后,将在控件2的文本框中显示出指定的文本
   
  在首页里,分别调用了控件1和控件2
   
   
  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" />
   
  这里放置一个按钮,然后用reference来引用控件2
  webcontrol.ascx.cs:
  protected void button1_click(object sender, eventargs e)
   {
   
   webusercontrol2 w = page.findcontrol("webusercontrol2_1") as webusercontrol2;
   w.text = "hello all!";
   
   }
   
   
  对于控件2:
  <%@ control language="c#" autoeventwireup="true" codefile="webusercontrol2.ascx.cs" inherits="webusercontrol2" %>
   
  <asp:textbox id="textbox1" runat="server"></asp:textbox>
  控件2的codebehind代码:
  public partial class webusercontrol2 : system.web.ui.usercontrol

  {
   protected void page_load(object sender, eventargs e)
   {
   
   }
   public string text
   {
   
   set { textbox1.text = value; }
   
   }
   
   
  }

TOP

返回顶部
AYBlue

Processed in 0.047946 second(s), 7 queries.

当前时区 GMT+8, 现在时间是 2009-1-10 11:56 京ICP备06054220号

清除 Cookies - 联系我们 - 163K.com - Archiver - WAP