473,763 Members | 1,893 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 3410
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
13732
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
2422
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
36879
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
1810
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
1978
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
1935
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
1439
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
2632
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
2299
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
10003
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
9943
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
8825
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7370
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
6643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5271
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
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
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
3529
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.