473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

User Controls - My Confusions

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.......
Nov 19 '05 #1
7 1275
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....
"Mr Newbie" <he**@now.com> wrote in message
news:OV**************@TK2MSFTNGP12.phx.gbl...
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.......

Nov 19 '05 #2
: 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
--
Cheers,
Gaurav Vaish
http://mastergaurav.org
----------------------
Nov 19 '05 #3
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
"Gaurav Vaish" <g_***********************************@gmail.com > wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
: 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
--
Cheers,
Gaurav Vaish
http://mastergaurav.org
----------------------

Nov 19 '05 #4
: 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.
--
Cheers,
Gaurav Vaish
http://mastergaurav.org
-----------------------------
Nov 19 '05 #5
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.


"Gaurav Vaish" <g_***********************************@gmail.com > wrote in
message news:OB****************@tk2msftngp13.phx.gbl...
: 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.
--
Cheers,
Gaurav Vaish
http://mastergaurav.org
-----------------------------

Nov 19 '05 #6
: 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.
--
Cheers,
Gaurav Vaish
http://mastergaurav.org
-----------------------------
Nov 19 '05 #7
that does not work 4 me

"Gaurav Vaish" <g_***********************************@gmail.com > wrote in
message news:OK****************@tk2msftngp13.phx.gbl...
: 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.
--
Cheers,
Gaurav Vaish
http://mastergaurav.org
-----------------------------

Nov 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
40
by: Paul Davis | last post by:
Hi all, I'm building some style sheets and trying to play the old game of balancing designer pixel perfection and still allowing users to adjust their font sizes. The compromise I've made with the...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
0
by: sonuindia88 | last post by:
I am preparing c for entrance examinations..I am reading Ritchie and Deitel and practising questions from Yashwant Kanetkar...Here are some of the confusions i am having..your help is required.... ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.