1

I'm working with a container which accepts a collection of server controls. The server control accepts a collection of user controls.

<Container>
  <Servercontrol>
    <UsercontrolA/>
    <UsercontrolB/>
    <UsercontrolC/>
    <UsercontrolD/>
  </Servercontrol>
  <Servercontrol>
    <UsercontrolE/>
  </Servercontrol>
</Container>

This will essential render out to be two columns in a table, with a header row, and a body row. The body row of the left column will have 4 user controls, the right will have 1.

The problem is that I want the programmers to be able to add any user control into this container.... but the user controls aren't rendered unless they have added the appropriate register tags.

<%@ Register TagPrefix="C" TagName="A" Src="~/Controls/UsercontrolA.ascx" %>
<%@ Register TagPrefix="C" TagName="B" Src="~/Controls/UsercontrolB.ascx" %>
<%@ Register TagPrefix="C" TagName="C" Src="~/Controls/UsercontrolC.ascx" %>
<%@ Register TagPrefix="C" TagName="D" Src="~/Controls/UsercontrolD.ascx" %>
<%@ Register TagPrefix="C" TagName="E" Src="~/Controls/UsercontrolE.ascx" %>

Also I'm not authorized to modify the web config.

Is there any workaround for this?

2
  • what do you mean you are not able to modify the web.config file? so what if you need to make modifications to the existing web application that you are supporting.. are you not allowed to add entry's into the web.config..? Commented Aug 20, 2013 at 13:55
  • @DJKRAZE The web.config is controlled by our architects, they really don't like us changing it. Commented Aug 20, 2013 at 14:03

1 Answer 1

2

Well you can load them dynamic on page load like this:

private const string UCFolderName = "~/Controls/";
...    
var usercontrolA= Page.LoadControl(UCFolderName + "UsercontrolA.ascx");

ServercontrolContainer.Controls.Add(UsercontrolA);

where ServercontrolContainer is a element in your .ascx file

Sign up to request clarification or add additional context in comments.

2 Comments

This does the trick, is there any way of doing this without knowing the directory to the web control? e.g. passing in the type?
No Unfortunately not, but you can use a prefix to make it "pretty" that is what i would do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.