469,924 Members | 1,429 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,924 developers. It's quick & easy.

User control instead of include

I'm trying to set up a user control and change some values from the aspx
page, but I keep running into trouble. What I really would like to do is be
able to set a Label.Text in the user control from an aspx page.

If I open up a static method to set the Label in the code-behind of the user
control(uc), then the code in the uc doesn't recognize that the label exists
(only if the method is a static (shared) method). If I set up a private
method, then the aspx page can't access the method, but then the uc does
recognize that the conrtol is there. Catch 22.

The old ASP way, I would just set the variable to the correct value on the
ASP page and the include would write it in a <%=SOMEVAR%>. How do I
accomplish this with user controls, or in this case, should I just resort to
an include file?

Andrea
Nov 18 '05 #1
4 1191
Your second paragraph made my head hurt from trying to figure out what it
meant, so I'm just going to ignore that. Let's just stick to changing a
property of a User Control in a WebForm. This is all object-oriented, so
let's start by talking about what these Controls are, which is objects. The
Page is an object/class, as is the WebForm contained in the Page, as is any
User Control that you add to a WebForm. Because these are all classes, there
are hidden (protected/private) and accessible (public) members. In order to
access a property or field in the User Control, you must have a public
property or field in the User Control which exposes it (makes it accessible
to other classes).

Next, you need to grab a reference to the User Control to set that
property/field's value. This can be done using the Control.FindControl()
method, which takes a string as its argument, representing the id property
of the Control being sought. The FindControl() Method searches the Controls
Collection of a Control and finds the Child Control that has the id that is
passed, and returns a reference to that Control. It works only on the
immediate Children of a Control, so you need to identify the Control that
contains it. Most likely, this will be the WebForm (not the Page).

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Andrea Williams" <an*******@hotmail.IHATESpam.com> wrote in message
news:#R**************@TK2MSFTNGP10.phx.gbl...
I'm trying to set up a user control and change some values from the aspx
page, but I keep running into trouble. What I really would like to do is be able to set a Label.Text in the user control from an aspx page.

If I open up a static method to set the Label in the code-behind of the user control(uc), then the code in the uc doesn't recognize that the label exists (only if the method is a static (shared) method). If I set up a private
method, then the aspx page can't access the method, but then the uc does
recognize that the conrtol is there. Catch 22.

The old ASP way, I would just set the variable to the correct value on the
ASP page and the include would write it in a <%=SOMEVAR%>. How do I
accomplish this with user controls, or in this case, should I just resort to an include file?

Andrea

Nov 18 '05 #2
Ok, at first I didn't understand your explaination, but a search on the
FindControl() method in the MSDN Lib got me thinking and experimenting, now
I understand.

THANKS! I am now able to do what I need! :-))

Andrea

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Your second paragraph made my head hurt from trying to figure out what it
meant, so I'm just going to ignore that. Let's just stick to changing a
property of a User Control in a WebForm. This is all object-oriented, so
let's start by talking about what these Controls are, which is objects. The Page is an object/class, as is the WebForm contained in the Page, as is any User Control that you add to a WebForm. Because these are all classes, there are hidden (protected/private) and accessible (public) members. In order to access a property or field in the User Control, you must have a public
property or field in the User Control which exposes it (makes it accessible to other classes).

Next, you need to grab a reference to the User Control to set that
property/field's value. This can be done using the Control.FindControl()
method, which takes a string as its argument, representing the id property
of the Control being sought. The FindControl() Method searches the Controls Collection of a Control and finds the Child Control that has the id that is passed, and returns a reference to that Control. It works only on the
immediate Children of a Control, so you need to identify the Control that
contains it. Most likely, this will be the WebForm (not the Page).

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Andrea Williams" <an*******@hotmail.IHATESpam.com> wrote in message
news:#R**************@TK2MSFTNGP10.phx.gbl...
I'm trying to set up a user control and change some values from the aspx
page, but I keep running into trouble. What I really would like to do is
be
able to set a Label.Text in the user control from an aspx page.

If I open up a static method to set the Label in the code-behind of the user
control(uc), then the code in the uc doesn't recognize that the label

exists
(only if the method is a static (shared) method). If I set up a private
method, then the aspx page can't access the method, but then the uc does
recognize that the conrtol is there. Catch 22.

The old ASP way, I would just set the variable to the correct value on

the ASP page and the include would write it in a <%=SOMEVAR%>. How do I
accomplish this with user controls, or in this case, should I just

resort to
an include file?

Andrea


Nov 18 '05 #3
Ok, another problem, FindControl only seems to work on Web Controls and not
HTML controls.... Am I missing something?

Andrea
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Your second paragraph made my head hurt from trying to figure out what it
meant, so I'm just going to ignore that. Let's just stick to changing a
property of a User Control in a WebForm. This is all object-oriented, so
let's start by talking about what these Controls are, which is objects. The Page is an object/class, as is the WebForm contained in the Page, as is any User Control that you add to a WebForm. Because these are all classes, there are hidden (protected/private) and accessible (public) members. In order to access a property or field in the User Control, you must have a public
property or field in the User Control which exposes it (makes it accessible to other classes).

Next, you need to grab a reference to the User Control to set that
property/field's value. This can be done using the Control.FindControl()
method, which takes a string as its argument, representing the id property
of the Control being sought. The FindControl() Method searches the Controls Collection of a Control and finds the Child Control that has the id that is passed, and returns a reference to that Control. It works only on the
immediate Children of a Control, so you need to identify the Control that
contains it. Most likely, this will be the WebForm (not the Page).

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Andrea Williams" <an*******@hotmail.IHATESpam.com> wrote in message
news:#R**************@TK2MSFTNGP10.phx.gbl...
I'm trying to set up a user control and change some values from the aspx
page, but I keep running into trouble. What I really would like to do is
be
able to set a Label.Text in the user control from an aspx page.

If I open up a static method to set the Label in the code-behind of the user
control(uc), then the code in the uc doesn't recognize that the label

exists
(only if the method is a static (shared) method). If I set up a private
method, then the aspx page can't access the method, but then the uc does
recognize that the conrtol is there. Catch 22.

The old ASP way, I would just set the variable to the correct value on

the ASP page and the include would write it in a <%=SOMEVAR%>. How do I
accomplish this with user controls, or in this case, should I just

resort to
an include file?

Andrea


Nov 18 '05 #4
Ok, another problem, FindControl only seems to work on Web Controls and not
HTML controls.... Am I missing something?

Andrea
"Kevin Spencer" <ke***@takempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Your second paragraph made my head hurt from trying to figure out what it
meant, so I'm just going to ignore that. Let's just stick to changing a
property of a User Control in a WebForm. This is all object-oriented, so
let's start by talking about what these Controls are, which is objects. The Page is an object/class, as is the WebForm contained in the Page, as is any User Control that you add to a WebForm. Because these are all classes, there are hidden (protected/private) and accessible (public) members. In order to access a property or field in the User Control, you must have a public
property or field in the User Control which exposes it (makes it accessible to other classes).

Next, you need to grab a reference to the User Control to set that
property/field's value. This can be done using the Control.FindControl()
method, which takes a string as its argument, representing the id property
of the Control being sought. The FindControl() Method searches the Controls Collection of a Control and finds the Child Control that has the id that is passed, and returns a reference to that Control. It works only on the
immediate Children of a Control, so you need to identify the Control that
contains it. Most likely, this will be the WebForm (not the Page).

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
"Andrea Williams" <an*******@hotmail.IHATESpam.com> wrote in message
news:#R**************@TK2MSFTNGP10.phx.gbl...
I'm trying to set up a user control and change some values from the aspx
page, but I keep running into trouble. What I really would like to do is
be
able to set a Label.Text in the user control from an aspx page.

If I open up a static method to set the Label in the code-behind of the user
control(uc), then the code in the uc doesn't recognize that the label

exists
(only if the method is a static (shared) method). If I set up a private
method, then the aspx page can't access the method, but then the uc does
recognize that the conrtol is there. Catch 22.

The old ASP way, I would just set the variable to the correct value on

the ASP page and the include would write it in a <%=SOMEVAR%>. How do I
accomplish this with user controls, or in this case, should I just

resort to
an include file?

Andrea


Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Seth Guard | last post: by
10 posts views Thread by BBM | last post: by
2 posts views Thread by Murphy | last post: by
5 posts views Thread by Rajani | last post: by
4 posts views Thread by Mark | last post: by
2 posts views Thread by edsuslen | last post: by
2 posts views Thread by tshad | last post: by
3 posts views Thread by remy.bur | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.