2013-02-13 52 views
2

我在我的项目中用tab容器创建了一个用户控件。我想从aspx页面访问标签容器,原因是禁用了一些标签。例如,我需要从aspx页面动态地隐藏第一个标签页和第三个标签页。因为我为不同的页面使用相同的用户控件。请帮我解决这个问题。如何访问后面的aspx代码中的用户控件控件

<%@ Register TagPrefix="cust" TagName="Creation" Src="~/Cust_Creation.ascx" %> 
<div> 
    <cust:Creation ID="uc_more_pack" runat="server" /> 
</div> 

+1

http://stackoverflow.com/questions/2257829/access-child-user-controls-property-in-parent-user-control – 2013-02-13 06:59:35

回答

1

在usercontrol上创建公共属性: 例如:

public bool ShowTab1 {get; set;} 
public bool ShowTab2 {get; set;} 
public bool ShowTab3 {get; set;} 
public bool ShowTab4 {get; set;} 

从.aspx.cs页面然后设置:

protected void Page_Load(object sender, System.EventArgs e) 
{ 
    usercontrol1.ShowTab1 = false; 
    usercontrol1.ShowTab2 = true; 
    usercontrol1.ShowTab3 = false; 
    usercontrol1.ShowTab4 = true; 
} 

使用属性来设置控件在用户控件:

protected void Page_Load(object sender, System.EventArgs e) 
{ 
    Tab1.Visible = ShowTab1; 
    Tab2.Visible = ShowTab2; 
    Tab3.Visible = ShowTab3; 
    Tab4.Visible = ShowTab4; 
} 
+1

谢谢你的朋友。它工作正常.. – user1951007 2013-02-13 09:23:51

3

增加您的用户控件的公共方法,这将是通过网页或控制消费用户控件访问。此方法可以采取任何参数来确定子选项卡容器的状态。

public void SetTabStatuses (bool tab1Enabled, bool tab2Enabled...){/* set status here */} 

public void SetTabStatuses (SomeStatusEnum status) {/* set status here */} 

对待用户控件作为一个对象,你已经加入到它应该考虑到对象上的字段的控件。我建议的方法允许你封装他们的行为。

相关问题