[英]how to access the user control controls in aspx code behind
我在項目中使用tab容器創建了一個用戶控件。 我想從aspx頁面訪問選項卡容器,原因是禁用了一些選項卡。 例如,我需要從aspx頁面動態隱藏第一個選項卡和第三個選項卡。 因為我對不同的頁面使用相同的用戶控件。 請幫我解決這個問題。
<%@ Register TagPrefix="cust" TagName="Creation" Src="~/Cust_Creation.ascx" %>
<div>
<cust:Creation ID="uc_more_pack" runat="server" />
</div>
一世
在您的用戶控件上添加一個公共方法,可以通過頁面或控件來訪問您的用戶控件。 此方法可以采用您想要的任何參數來確定子選項卡容器的狀態。
public void SetTabStatuses (bool tab1Enabled, bool tab2Enabled...){/* set status here */}
要么
public void SetTabStatuses (SomeStatusEnum status) {/* set status here */}
將用戶控件視為對象,並且應將您添加到其中的控件視為該對象上的字段。 我建議的方法是允許你封裝他們的行為。
在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;
}
使用該屬性在UserControl中設置控件:
protected void Page_Load(object sender, System.EventArgs e)
{
Tab1.Visible = ShowTab1;
Tab2.Visible = ShowTab2;
Tab3.Visible = ShowTab3;
Tab4.Visible = ShowTab4;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.