473,411 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 1344
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Seth Guard | last post by:
(Sorry for the cross-post, but no one responded to my other thread.) When developing a web page in VS.Net, I see everything in WYSIWYG format. And when I am creating a User Control, I see that...
10
by: BBM | last post by:
Hi, I have been developing with C# User Controls and occasionally have a problem where I "lose" a control from the design surface of the User Control. The controls that I am using to build my...
2
by: Murphy | last post by:
I have a number of pages that include basic html such as "About" and I have delegated the upkeep of these pages to various staff. I am concerned about having the staff edit the aspx file directly...
5
by: Rajani | last post by:
Hello, I have a strange problem. I want to check the privilege of the login user on each page and allow to display if has suff. priv. I am storing the privilege is session variable. I am...
4
by: Mark | last post by:
I am setting up an asp.net site using the Ektron CMS. Every page in the site uses the same basic template, with the look of the page changing via CSS and an id on the body. The id is shared by all...
20
by: Alan Silver | last post by:
Hello, In classic ASP, I used to use two include files on each page, one before and one after the main content, to provide a consistent layout across a web site. That way I could just change the...
2
by: edsuslen | last post by:
I am trying to migrate from asp to asp.net and having problem replacing include with user control. On every page we have include asp that have some common asp code that I need to run before the...
2
by: tshad | last post by:
User VS 2003, I would like to use User Controls to control the page look with 3 User Controls (...PageTop.ascx, ...NavigateTop.ascx and PageBottom.ascx). I would put the Content User control...
3
by: remy.bur | last post by:
Hi, When adding an anchor (which is runat="server") in a user control, the link generated will be based on the user control location and not on the page which is using the control location. For...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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
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...

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.