Monday, March 26, 2012

Why my UserControl doesnt show up?

I'm a little lost here, in ASP 1.1 I used to pass parameters to usercontrols like this:
MyControl control = (MyControl)LoadControl("Control.ascx");
control.Whatever = "Hello";
But now I can't see my user control class in ASP 2, any ideas what happened?
How can I do this?

Hello.

What do you mean by when you say you can't see your control? where are you putting that code?


I can't see the class name of my UserControl, so I cannot cast it and set its properties.

Hello.

sorry, i hadn't noticed that in your code you're not using the namespace. By default, the classes which represent pages and user controls are placed inside the ASP namespace. So, try doing something like this:

ASP.MyControl ctl = this.LoadControl( "..." ) as ASP.MyControl;

a few days ago there was discussion about this kind of things here, but I can't seem to find the posts...i was also under the impression that doing something like that was only possible after referencing the control (by using the @.register or @.references directive), but it seems like I was wrong.


This is really frustating, but no, I only see webforms and masterpages under ASP, I dont see any of my UserControls.


What the hell, even if you put the control in a custom namespace, itdoesn't show up, not even in the object browser... actually, onlyclasses within the App_Code directory are shown the rest is not showed.
Whats going on here?


Hi.<br /
I think the thread you are referring to might be <a href='http://forums.asp.net/921691/ShowPost.aspx'>http://forums.asp.net/921691/ShowPost.aspx</a>.<br /> But according to 'adec' in that thread we are wrong experiencing this behavior.

Hello.

Yep, that's the thread.

It's tue that there's no intellisense, but it works (at least I've built a small sample where I'm able to load the control and cast it to it's type without using the register tag). if you really nead intellisense, then add a register or references directive to the page. if you can live without it then just don't forget to reference the namespace ASP.


I'm sorry, but I can't get it to work, how can I add a register/reference directive and still load it programatically?

Hello.

Yep, I've also added another post on the other thread saying that i'm not beeing able to do that either. well, think that it only works is if you add the reference directive at the top of the page. I'd really like to know what happened when i performed the other test. well, i'm positive that it worked then...i'd really lke to get a definitie explanation on this. anyone?


Hi.

I have no idea of how MS is going to solve/explain/work around this issue, but one way that worked for me was to let the controls that I would like to load dynamically either inherit from a common base or implement an interface.
This base class or interface is in the App_Code assembly and therefor known by the page at editing/compile time of the page loading the control.
Hopes this helps.
Hello.
Well, my problem is understanding why it worked in the 1st place without using the reference or register directive(if you read the other thread you'll notice that I initially stated that you needed to include reference or register directive so that it could work; however, after trying andre's code i saw it working in my machine). i'd really appreciate if someone could give me a definitive answer...
Me too!, I just wanna get it to work, this is absurd!

Thats right, the only way it works is using the reference tag.

0 comments:

Post a Comment