How does user controls's instances work?

C

cavassinif

Hi,

I have my user control called: WebUserControl1.ascx

I have my web page called: WebForm1.aspx

I drag WebUserControl1.ascx to WebForm1.aspx.

The following tags were added to my page:

<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>

<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>

Now, I want to call a method on the control instance
"WebUserControl11", I use this code on my web page:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

------> WebUserControl11.RefreshControlData() <-----

End Sub

Error, WebUserControl11 is not declared, it doesn't exists on my page
scope.

What I'm missing here or what I don't understand...why I can't access
the control instance in my code part of page?

Best Regards
Fabio
 
S

Steve Lutz

Hi Fabio,

In your code behind class for the page, you need to declare a protected
variable for the control.

Give it the same name and type as the control. Sorry, but I don't know the
Syntax for VB, but for C#, it would be:

protected WebUserControl1 WebUserControl11;

So in VB, it might be something like (I'm just guessing)
dim protected WebUserControl11 as WebUserControl1;

Once you do that, ASP.NET will create the instance based on the ASPX page's
control. It is important that they are named the same.

HTH

Steve
 
C

cavassinif

Great! I know that this should be very simple

Thanks Steve, this was annoying Me

Best Regards
Fabio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top