asp.net2.0中不同web控件之间的相互调用


  本文标签:asp.net web 控件

  在asp.net 2.0中,要在不同的web控件之间互相调用,必须要<%@ reference virtualpath="另一控件名称">来引用,举例如下:

  

  default.aspx:





  我们要实现的是,按下控件1的按钮后,将在控件2的文本框中显示出指定的文本

  在首页里,分别调用了控件1和控件2:

  

  webcontrol.ascx:

  <%@ control language="c#" autoeventwireup="true" codefile="webusercontrol.ascx.cs"

  inherits="webusercontrol" %>

  <%@ reference virtualpath="'/webusercontrol2.ascx" %>

  

  这里放置一个按钮,然后用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" %>

  控件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; }
}
}


(责任编辑 火凤凰 sunsj@51cto.com  TEL:(010)68476636-8007)