Wednesday, August 13, 2008 1:01 PM djsolid

The @Reference Directive

When I want to add a user control to a page I use the @Register directive at the top of the page. and the I add the control to the markup. But what if I want to add a user control programmatically but I have no reference of it via the markup? That is where the @Reference directive comes to action.

What's the story?

We have a user control with the name myUserControl.aspx

1

We add at the top of the page the control we want to programmatically add like this...

2

And finally the controls is being added to the page like this...

3

In general, when we declare a control in the page layout we use @Register and when we create the control programmatically we use @Reference.

That's it ...

kick it on DotNetKicks.com

Filed under: , , , ,

Comments

# The @Reference Directive

Wednesday, August 13, 2008 6:02 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# funny wallpaper » The @Reference Directive

Wednesday, August 13, 2008 6:17 AM by funny wallpaper » The @Reference Directive

Pingback from  funny wallpaper » The @Reference Directive

# carnewsservice.info » The @Reference Directive

Wednesday, August 13, 2008 7:02 AM by carnewsservice.info » The @Reference Directive

Pingback from  carnewsservice.info » The @Reference Directive

# re: The @Reference Directive

Wednesday, August 13, 2008 8:57 AM by AndrewSeven

It looks like you are using a web application project; In my experience, the @Reference is not needed for what you have shown.

# re: The @Reference Directive

Wednesday, August 13, 2008 9:15 AM by djsolid

If you try

myUserControl m = new myUserControl();

Controls.Add(m);

it won't work. Neither in a web application nor in a website project. More over in a web application you compile the above statement but the control won't be added to the page but in a website the above statement wont even compile if the Reference directive is not added.

I am not sure if you mean that.

# re: The @Reference Directive

Sunday, August 24, 2008 2:24 PM by Dimitris-Ilias Gkanatsios

No, I believe he means that you can use LoadControl method without using the @Reference directive at all...

Leave a Comment

(required) 
(required) 
(optional)
(required)