469,592 Members | 1,674 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

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 3175
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Richard Dixson | last post: by
6 posts views Thread by William Parker | last post: by
3 posts views Thread by Mr Newbie | last post: by
1 post views Thread by zhuang | last post: by
2 posts views Thread by krallabandi | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.