User control instead of include

A

Andrea Williams

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
 
K

Kevin Spencer

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.
 
A

Andrea Williams

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
 
A

Andrea Williams

Ok, another problem, FindControl only seems to work on Web Controls and not
HTML controls.... Am I missing something?

Andrea
 

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

Top