User Controls - My Confusions

M

Mr Newbie

Hi There,



Here I am messing about with User Controls and I seem to have a conundrum on
my hands which I'm sure you chaps will unravel for me in the blink of an
eye.



When one drags a user control on to the canvas of the form designer, it
creates the HTML associated with the user control, but unlike the toolbox
controls it does not create the code for the control in the associated
forms construction code ( VB in my case ).



So in order to instantiate the control for user, I need to do this my self,
fine so far . . . .



All I want to do is to press a button on the containing form and have a
message displayed in a Literal control which is inside the User Control.
"Easy you say"



OK, bear with me . . . . . .



User controls are not loaded until after the form load event so you cannot
access them at this time and need to wait for the pre-render event right
????????



The buttons click event runs prior to the pre-render event, so how the hell
are you supposed to do this. In order to access the control at all I have to
declare a class variable of that type in the form but cant instantiate it
until the pre-render event.



I'm sure I have this all out of whack, and it's a lot simpler than I am
making it, but could someone illuminate my poor fuzzled brain on the right
approach for using User Controls in this sense. I'm sure microsoft made it
simple and My brain is trying to make it difficult.



Thank You





Mr N.......
 
M

Mr Newbie

I appear to be talking partial rubbish.

It appears that the above text is incorrect and that the UserControl is
actually instantiated in the webforms collection of controls prior to the
Page_Load event , since one can access it via a me.FindControl("Control
name") during the same.

But it really seems awkward that you have to declare a variable of the type
yourself and then find it during the page load before you can use it.

Surely, once the code behind has been compiled, the object and its methods
should be made available to the containing page much the same as the other
controls brought from the toolbox.

Where have I gone wrong in my understanding?

Mr N....
 
G

Gaurav Vaish

: It appears that the above text is incorrect and that the UserControl is
: actually instantiated in the webforms collection of controls prior to the
: Page_Load event , since one can access it via a me.FindControl("Control
: name") during the same.

Yes. That's the way it is supposed to be. The control tree is available
even during the Init event. The only thing that differs is that the controls
may not be loaded etc.

Controls are instantiated, IDs applied and the tree is available
immediately. They are Initialized / Loaded later on.


: But it really seems awkward that you have to declare a variable of the
type
: yourself and then find it during the page load before you can use it.

The declaration of "protected" is important. Just try to change it to
"private" and you should be able to get an idea of what's going on...


Mr N.... it would be great if you could let us know your name.. only if
you want. :D
 
M

Mr Newbie

Thanks for your reply Gaurav,

I still dont understand why intellisense cannot access the control at design
time like standard controls, can you illuminate ?

Best Regards
 
G

Gaurav Vaish

: Thanks for your reply Gaurav,
:
: I still dont understand why intellisense cannot access the control at
design
: time like standard controls, can you illuminate ?

Why it can't.. I won't be able to tell.

But as what I understand, it makes sense to access the control's
properties from code-behind rather than from the ascx itself.

Yes... I can very well get the exposed (read: public) properties from
the code-behind.
 
M

Mr Newbie

I think we misunderstand each other.

In order to user a Web User Control, one has to manually declare it at class
level and then assign the control to it at some point. This is not possible
at design time. In other words I have to do something like this. ( Unless Im
mistaken )

Protected WithEvents MyControl As MyControlIDAsAssignedInTheHtml


Page Load . . .

MyControl = Me.FindControl("MyControlIDAsAssignedInTheHtml")


Now I can use it. My Question is why I have to go to all this bother, when
standard controls off the toolbox are added by the designer and are
immediately accessable at design time.

This is the answer I am looking for. . .

Cheers MR N.
 
G

Gaurav Vaish

: In order to user a Web User Control, one has to manually declare it at
class
: level and then assign the control to it at some point. This is not
possible
: at design time. In other words I have to do something like this. ( Unless
Im
: mistaken )

Yes. You are mistaken.. at least from the code, it seems so. Let me give
my try... :D

: Protected WithEvents MyControl As MyControlIDAsAssignedInTheHtml
:
: Page Load . . .
:
: MyControl = Me.FindControl("MyControlIDAsAssignedInTheHtml")

No. You don't need to do that. Firstly, there's something wrong with the
VB code (Protected ...)

Here's how it goes...

1. In CodeBehind (for aspx):
Protected WithEvents idThatIWant As CodeBehindClassForASCX

And in Page_Load...

idThatIWant.DoSomething()


2. In aspx page:
<%@ Register TagPrefix="pref" TagName="SomeName" Src="file.ascx" %>

followed by:

<pref:SomeName id="idThatIWant" runat="server" />

That's it.
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top