问:
比如说:我用一个header.ascx作为页首导航栏,用一个leftnv.ascx作为左边的导航栏,这样,在leftnv.ascx里能访问header.ascx里的控件吗?
谢谢你的赐教
______________________________________________________________________________________________
答1:
分别给这两个控件添加各自的属性.然后在代码中声明这两个控件,就可以和Web页上的其它控件一样使用了.
______________________________________________________________________________________________
答2:
感谢您使用微软产品。
在您的aspx代码中,把UserControl (ascx文件)当做对象,您需要注意下面的几点:
1。 请确认您的ascx文件中声明的有对应该UserControl的类。
如果您使用Code-Behind, 则在您的ascx.vb文件中,会有该类。如果您的UserControl的代码和HTML在同一个ascx文件中,您需要声明下面语句,注册类:
<%@ Control className="MyUserControl" %>
2。 在ascx对应的类中,声明public属性,用来传出您要控制的属性,比如:
public System.DateTime SelectDate
{
get
{
return this.Calendar1.SelectedDate;
}
}
3。 在您的aspx文件中,不要使用<%@Register...%>,而使用下面的声明语句:
<%@ Reference control="MyUserControl.ascx"%>
4。 在您的aspx文件对应的类中,声明下面的变量,用来保存将要生成的UserControl对象:
protected Control con;
4. 在您的Page_Load事件中,加入下面的代码:
con=LoadControl("MyUserControl.ascx");
//"form1"是您的aspx文件中<form runat=server>的ID, 当然,您也可以把该对象放入其他container.
Control frm=this.FindControl("form1");
frm.Controls.Add(con);
Label1.Text=( (MyUserControl) con).SelectDate.ToString();
this.con.Visible=false;
this.con.Visible=true;
5.现在,您就可以像控制aspx中的Textbox对象一样,控制UserControl对象。
同时,请您参阅下面的帖子:
http://www.csdn.net/expert/topic/970/970394.xml?temp=.5522272
希望对您有所帮助。
-微软全球技术中心