2013-02-13 105 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 */} 

對待用戶控件作爲一個對象,你已經加入到它應該考慮到對象上的字段的控件。我建議的方法允許你封裝他們的行爲。

相關問題