简体   繁体   中英

how to access the user control controls in aspx code behind

I created a user control with tab container in my project. I want to access the tab container from aspx page for the reason of disable the some tabs. For example i need to hide the first tab and third tab dynamically from aspx page. Because i am using the same user control for different page. Please help me to fix this issue.

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

I

Add a public method on your user control which will be accessible via the page or control consuming your user control. This method can take whatever parameters you would like to determine the status of the child tab containers.

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

or

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

Treat the user control as an object and the controls you've added to it should be considered fields on that object. The method(s) I suggest is allowing you to encapsulate their behavior.

Create public property on usercontrol: Eg.

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

Set then from .aspx.cs page:

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

Use the property to set the controls in UserControl:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM