473,881 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1368
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.FindCon trol()
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*******@hotm ail.IHATESpam.c om> wrote in message
news:#R******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP10.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.FindCon trol()
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*******@hotm ail.IHATESpam.c om> wrote in message
news:#R******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP10.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.FindCon trol()
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*******@hotm ail.IHATESpam.c om> wrote in message
news:#R******** ******@TK2MSFTN GP10.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******** ********@TK2MSF TNGP10.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.FindCon trol()
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*******@hotm ail.IHATESpam.c om> wrote in message
news:#R******** ******@TK2MSFTN GP10.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
320
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 drawn the same way. However, when I drag the User Control onto my main page, I see a gray box where the User Control should be. (I was expecting to see my User Control actually *rendered* on the Design screen, similar to how FrontPage renders...
10
2713
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 User Controls are themselves custom controls. Occasionally, when I change something in either the User Control and/or one of the custom controls that are on it and recompile, when I go to the "Visual" design surface of the User Control, the...
2
1180
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 as there is more than just the html page content in there and I don't want it stuffed up. Until now I have put the content in an aspx that is placed on the aspx however as the content is mere html should I simply put it in a text file and use an...
5
2284
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 checking on the page_load event like if session("uid")="" then response.redirect("loginverify.aspx?er=nlog") elseif session("priv")<>0 then response.redirect("loginverify.aspx?er=nsad")
4
1822
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 pages in a subsection, e.g. everything in /about/ needs to have <body id="about">. In the past what I've done is set a global variable in index.asp, then include the template to render the page: <% pageid = "about"
20
2436
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 include files to change the layout. When I came to ASP.NET, I used user controls to do a similar thing. I have just been looking at master pages, and it looks like they do the same thing. If so, is there any advantage in using them over the...
2
1202
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 page code kicks in, but user control code is always running after the page code. I am even registering and placing user control before my page initialization, but still it's running after. <%@Register TagPrefix="FindSessionUC"...
2
1870
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 between the NavigateTop and PageBottom User controls. These files be something like: sdhcPageTop.ascx, ft2PageTop.ascx, sbPageTop.ascx, macPageTop.ascx etc. I would have the same files for the NavigateTop.ascx and PageBottom.asc. The ones that...
3
2858
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 example, if the user control is located in an include folder, here is what I will get: <a href="include/home.aspx"> instead of <a href="home.aspx">
0
11096
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10812
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10400
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7953
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5780
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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 we have to send another system
2
4194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.