Sign in to follow this  
CyberSlag5k

[web] ASP.Net - Generic user control

Recommended Posts

I've made an ASP.Net user control that accepts a generic type. Because of this, I have to dynamically add the control to the page. I've decided to use a PlaceHolder to do this. However, I cannot get the control to render. This is what I'm trying to do:
RuleSetControl<ActionControl> ruleSetControl = LoadControl(typeof(RuleSetControl<ActionControl>), null) as RuleSetControl<ActionControl>;

PlaceHolder.Controls.Add(ruleSetControl);

RuleSetControl is the control that I've created, and ActionControl is the control that I wish to pass to it as a generic. This compiles, but nothing shows up on the screen. I've determined that the problem is that overload to LoadControl, as when testing the following with my very basic ActionControl (which just contains a button):
Control actionControl = LoadControl("ActionControl.ascx"); // This renders the control when added to the PlaceHolder
Control actionControl = LoadControl(typeof(ActionControl), null); // This does not

I've seen others run into this same problem, and the response always seems to be "Just use the overload that takes a string". That doesn't work for me, as there's no way to specify the generic type like that. Does anybody either: 1. What I need to get the overload of LoadControl that takes a type to work properly or 2. How else I can get a generic user control on screen? Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this