如何访问后面的aspx代码中的用户控件控件

时间:2013-02-13 06:51:36

标签: c# asp.net

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

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

2 个答案:

答案 0 :(得分:3)

在您的用户控件上添加一个公共方法,该方法可通过使用您的用户控件的页面或控件访问。此方法可以采用您想要的任何参数来确定子选项卡容器的状态。

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

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

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

答案 1 :(得分: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;
}

使用该属性设置UserControl中的控件:

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