2

I have a UserControl that I need to add dynamically. I tried to follow this MSDN article, but I'm not having any success.... http://msdn.microsoft.com/en-us/library/c0az2h86.aspx

The UserControl is basically an image gallery, and it loads some pictures based on an ID. My idea was to make this ID available as a property. Then when I create an instance of the control, I could set this ID and add it to the form.

I added a reference to the control in the .aspx page that will use it, like this:

<%@ Reference Control="~/PictureGallery.ascx" %>

And in the UserControl I added a ClassName like this:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PictureGallery.ascx.cs"
Inherits="PictureGallery" ClassName="PictureGallery" %>

When I try to create an instance in the .aspx.cs like the article suggests, Dim gallery As ASP.PictureGallery, I get an "Type ASP.PictureGallery is not defined".

The article mentions a namespace, ASP, and I tried importing it to the .aspx.cs with no luck. So, I'm not able to get a reference to the UserControl.

How can it be fixed?

3 Answers 3

7

It sounds like you are confusing two separate ways of working with a UserControl.

One way is to register the control on your page so that you can put it into the page at Design Time e.g.

<div>
    <asp:PictureGallery id="myGallery" runat="server" MyProperty="SomeValue">  
    </asp:PictureGallery>
</div>

The second is programmatically (or dynamically) adding it into the page at runtime in your code behind. If so, then you need to use the LoadControl function which is mentioned in the sample. You do not need to register the control in the aspx file if you do this. e.g.

Dim gallery as PictureGallery = LoadControl("~/PathToControl/gallery.ascx")
gallery.MyProperty = "SomeValue"
placeHolder.controls.add(gallery)

edit
What is the class name of the control in the code behind...something like this:

Partial Public Class MyControlsClassName
    Inherits System.Web.UI.UserControl

That is the type you need to use when you declare it. Is it within a namespace perhaps?

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

7 Comments

Yup, I know this. But doing as you suggest, I get "Type PictureGallery is not defined", and I can't access gallery.MyProperty, not with intellisense anyway. This usually tells me that something is wrong.
@Soeren, see my edit...its easier to put code in the answer than comment
Apparently I don't have edit privileges, so I have to put the code here. The classname is 'public partial class PictureGallery : System.Web.UI.UserControl' No namespace...
@Soeren Is your project C# or VB.net? Your files indicate c#, but you say that Dim etc doesn't work which is vb.net code...so you want me to change my question to be c#?
The usercontrol is C#, and the page that uses it, is a VB page. So the code I need to instantiate the control is VB..
|
0

I don't think you've placed the control in your code behind. You may well have created the reference, but do you have a tag such as <asp:PictureGalary id="gallary"></asp:PictureGalary> anywhere in your aspx?

2 Comments

This is because I add the UserControl programmatically by referencing the ClassName using <%@ Reference Control="~/PictureGallery.ascx" %>.
What you suggest is adding the UserControl declaratively, and then I would need to register it instead
0

The ASP namespace is generated at run time- user controls get "compiled" as they are used by .aspx pages so this is why you get the error message "Type ASP.PictureGallery is not defined".

When adding user controls dynamically you should use the Page.LoadControl method:

Page.LoadControl("~/PictureGallery.ascx")

Comments

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.