473,624 Members | 2,216 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to access web control from code behind page?

I have a web control I made called header.ascx. It has its own properties
and methods I defined. But I cannot figure out how to access this control
from my code behind page.

I can create the web control just fine and script with it as needed from the
webform1.aspx page itself just fine, like this:

<%@ Page language="c#" Codebehind="Web Form1.aspx.cs" AutoEventWireup ="false"
Inherits="mysit e.WebForm1" %>
<%@ Register TagPrefix="uc1" TagName="header " Src="header.asc x" %>
<uc1:header Title="This is working!!" id="Header1"
runat="server"> </uc1:header>
<%
String mytitle = Header1.Title;
Header1.Title = mytitle + " abc";
%>
<uc1:footer id="Footer1" runat="server"> </uc1:footer>

But how can I set or get to the web control's properties from the code
behind's Page_Load event instead of directly in the .aspx file? For example
this does NOT work:

private void Page_Load(objec t sender, System.EventArg s e)
{
String mytitle = Header1.Title;
Header1.Title = mytitle + " abc";
}

When I try that code above I get this error:
"d:\inetpub\www root\mysite\Web Form1.aspx.cs(2 3): The type or namespace name
'Header1' could not be found (are you missing a using directive or an
assembly reference?)".

If I add a declaration above Page_Load like this "protected
System.Web.UI.U serControl Header1;" then the above error goes away, but then
it complains that Header1 doesn't contain a definition for "Title". This is
because by declaring Header1 in the web form code behind it is obviously
just creating it as a new, blank web control when in fact I am just looking
for a way to reference the one that is created in the .aspx file.

Bottom line is that I want to be able to reference the Header1 web control
created in the .aspx file from my code behind page, but I don't know how I
am supposed to reference or tie in to it. Can someone please help? Thank
you very much.

Nov 18 '05 #1
1 3399
Make sure you add a declaration for the control in your code-behind class.

Dale

"William Parker" <wi*****@nonosp am.com> wrote in message
news:Tb******** ************@co mcast.com...
I have a web control I made called header.ascx. It has its own properties
and methods I defined. But I cannot figure out how to access this control
from my code behind page.

I can create the web control just fine and script with it as needed from the webform1.aspx page itself just fine, like this:

<%@ Page language="c#" Codebehind="Web Form1.aspx.cs" AutoEventWireup ="false" Inherits="mysit e.WebForm1" %>
<%@ Register TagPrefix="uc1" TagName="header " Src="header.asc x" %>
<uc1:header Title="This is working!!" id="Header1"
runat="server"> </uc1:header>
<%
String mytitle = Header1.Title;
Header1.Title = mytitle + " abc";
%>
<uc1:footer id="Footer1" runat="server"> </uc1:footer>

But how can I set or get to the web control's properties from the code
behind's Page_Load event instead of directly in the .aspx file? For example this does NOT work:

private void Page_Load(objec t sender, System.EventArg s e)
{
String mytitle = Header1.Title;
Header1.Title = mytitle + " abc";
}

When I try that code above I get this error:
"d:\inetpub\www root\mysite\Web Form1.aspx.cs(2 3): The type or namespace name 'Header1' could not be found (are you missing a using directive or an
assembly reference?)".

If I add a declaration above Page_Load like this "protected
System.Web.UI.U serControl Header1;" then the above error goes away, but then it complains that Header1 doesn't contain a definition for "Title". This is because by declaring Header1 in the web form code behind it is obviously
just creating it as a new, blank web control when in fact I am just looking for a way to reference the one that is created in the .aspx file.

Bottom line is that I want to be able to reference the Header1 web control
created in the .aspx file from my code behind page, but I don't know how I
am supposed to reference or tie in to it. Can someone please help? Thank
you very much.

Nov 18 '05 #2

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

Similar topics

2
13725
by: adiel | last post by:
Hello, I am trying to access the properties and methods from a user control within the code-behind file for a webform but I am receiving the message: Name 'MenuBar1' is not declared It does not recognize the user control in the code behind... Here is the code for the user control: MenuBar.ascx:
5
2418
by: Dan | last post by:
We have a simple site. It's a frameset with two frames a left and a right. The left frame is essentially a list of records from a database (using a server-side repeater control). When you click on one of the items in the left frame, it targets the right frame and displays a form prefilled with information for the item you clicked. The problem is the left frame's list just shows the names of the items, and the name of the item is...
5
36846
by: Richard Dixson | last post by:
I created a new C# web application. Basically all I am trying to do is create a table that consists of a few rows with two columns in each. And then to set those columns to text values from my code behind. However I am not able to do this at all, I am going about this wrong, I think and need guideance. If this was just straight HTML I would do this: <table> <tr><td>field 1</td><td>value 1 set by code behind</td></tr>
6
535
by: William Parker | last post by:
I have a web control I made called header.ascx. It has its own properties and methods I defined. But I cannot figure out how to access this control from my code behind page. I can create the web control just fine and script with it as needed from the webform1.aspx page itself just fine, like this: <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="mysite.WebForm1" %> <%@ Register TagPrefix="uc1"...
9
1803
by: John Smith | last post by:
Hi, I have this problem. I have a page class in code behind called "WebForm1" and I have another class called "Class1". Now in Class1 I am creating ImageButton and I am adding event handler onClick, then this ImageButton is added to page - WebForm1. In WebForm1 I have my methods to deal with the data. When I click ImageButton, ImageButton_onClick is called in Class1. Now, my problem is that I want to call the method 'test' that is in...
2
1967
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page submit. I've read several post here and articles on the web on this topic. What little I have learned from them is that you have to pre-compile the usercontrol in order to access it in the aspx code behind. I did this, compiling the usercontrol...
3
1928
by: Mr Newbie | last post by:
I am messing around with Web User Controls at present and (think) I have discovered the following. 1.) The identifier for the control in the code behind must match the ID for the control on the page. 2.) The identifier must be delcared with a minimum access of 'Protected' in order to work. If the above is indeed correct, then I have made the following assumptions
1
1430
by: zhuang | last post by:
When we drag and drop any server control from toolbox to web form, the relevant control is added to code-behind automatically with access level to be protected. When we drag and drop a user control to a web form, if we want to use that user control in code-behind, then we need to declare it to be protected as well, otherwise it will not work properly, why??? I never pay attention to this untill I start to work with user control.
2
2624
by: krallabandi | last post by:
I have a hidden control <input id="FieldSortOrder" type="hidden" name="FieldSortOrder" runat="server"> in a ascx page. I want to store some value in this control in the code behind page. FieldSortOrder.Value = "xyz" I am getting compilation errors - 'FieldSortOrder' is not declared. The same thing is working good on aspx. how can it be done on ascx?
7
2289
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using FindControl the only way to do it?
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8677
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
8620
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...
0
8474
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
6110
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
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
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
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.