473,378 Members | 1,413 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,378 software developers and data experts.

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="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="mysite.WebForm1" %>
<%@ Register TagPrefix="uc1" TagName="header" Src="header.ascx" %>
<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(object sender, System.EventArgs e)
{
String mytitle = Header1.Title;
Header1.Title = mytitle + " abc";
}

When I try that code above I get this error:
"d:\inetpub\wwwroot\mysite\WebForm1.aspx.cs(23 ): 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.UserControl 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 3382
Make sure you add a declaration for the control in your code-behind class.

Dale

"William Parker" <wi*****@nonospam.com> wrote in message
news:Tb********************@comcast.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="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="mysite.WebForm1" %>
<%@ Register TagPrefix="uc1" TagName="header" Src="header.ascx" %>
<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(object sender, System.EventArgs e)
{
String mytitle = Header1.Title;
Header1.Title = mytitle + " abc";
}

When I try that code above I get this error:
"d:\inetpub\wwwroot\mysite\WebForm1.aspx.cs(23 ): 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.UserControl 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
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...
5
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...
5
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...
6
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...
9
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...
2
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...
3
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...
1
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...
2
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. ...
7
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.