如何在aspx代码后台访问用户控件控件

4

在我的项目中,我创建了一个带有选项卡容器的用户控件。我希望从aspx页面访问选项卡容器,以便于禁用某些选项卡。例如,我需要动态地在aspx页面隐藏第一个选项卡和第三个选项卡,因为我在不同的页面上使用相同的用户控件。请帮助我解决这个问题。

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

I


1
http://stackoverflow.com/questions/2257829/access-child-user-controls-property-in-parent-user-control - Brian Webster
2个回答

3
在您的用户控件上添加一个公共方法,该方法将通过使用您的用户控件的页面或控件进行访问。此方法可以采用任何参数来确定子选项卡容器的状态。
public void SetTabStatuses (bool tab1Enabled, bool tab2Enabled...){/* set status here */}

或者

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

将用户控件视为一个对象,而您添加到其中的控件应被视为该对象上的字段。我建议采用的方法是允许您封装它们的行为。


1
在用户控件上创建公共属性: 例如:
 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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接