2
Answers

Is it possible to call an .ascx control from an .ascx page?

Photo of Daniel Smith

Daniel Smith

16y
10.4k
1

Hi,

Is it possible to call an .ascx control from an .ascx page?

For example, I have the following code in a .ascx file:

<a href="../../HeadQuarters.aspx">

<a href="../../Processing.aspx">

<a href="../../Storage.aspx">

I had to create these .aspx files in order to get the href's to work. The problem is that they all contain the exact same code with a few exceptions. I would like to call each .ascx control file within the one.ascx file.

Using VS.net 2008

3.5 Framework

Any assistance you can provide would be greatly helpful.

Thanks!

Answers (2)

0
Photo of Daniel Smith
NA 3 0 16y

Thanks Paul and please forgive my ignorance.

So, I should create a Link Button control:

<asp:LinkButton ID="lkbHeadQuarters" runat ="server" OnClick="lkbHeadQuarters_Click"><i><b>See more pictures...&#187;</b></i></asp:LinkButton>

In addition to the user control you had suggested?

<-uc1:hq ID="myControl" runat="server" Visible="false" />

If so, then how do I handle this in the code behind?

Also, I have tried the link button in the past, however the event doesn't fire.

Regards,

Dan

0
Photo of Paul
NA 7 0 16y
You certainly can.  Funny enough I just put a project together last week and I'll provide some input that should help you along.

You would drop some link buttons on the "container" control (.ascx) then handle the click events in your code to do what you want based on the link button clicked.  So given this there are two options you have to load up a control.  You could:
  1. Dynamically add the controls to a panel within the "container" control.  You would need to drop a panel in the control page then clicking a link button  will load up the necessary control in to the panel.  The code would be something like the following:

         YourUserControl ctrl = new YourUserControl();
         Panel1.Controls.Add(ctrl);

  2. Register your controls in the "container" control page then drop then put these controls where you want them to show on your control page.  You would initially hide these controls then show the necessary control based on the link button click which would be handled in code.

    So on your control page you would register the control with the following:

         <%@ Register Src="~/UserControls/YourUserControl.ascx" TagName="YourControl" TagPrefix="uc1" %>

    You can then set the control with the following:

         <uc1:YourUserControl ID="_yourControl" runat="server" Visible="false" />
A few things to note...

With the first option you lose all view state on any postback, async or not, and your control will disappear from the page since the "container" control will be reloaded on postbacks. 

With the second option you do not lose any state within the controls and in fact all controls will execute regardless of which one is displayed.  Now keep in mind I stated "within the controls" as the "container" control will reload on any postbacks so the only necessary view state needed is to maintain which control should be visible, which would set on the link button click.  Then when the "container" control reloads you would just evaluate the view state to determine which control should be visible. By having the controls on the page you will have direct access to any public methods on the controls.  This means that instead of a standard IsPostback() check you could create a public method to execute the necessary code when the control is selected.  For that matter you can call any public methods at any given time.

Hope this helps.  By the way I went with option #2 if it wasn't clear by the book I wrote above...