Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How to access web control from code behind page?

Reply
Thread Tools

How to access web control from code behind page?

 
 
William Parker
Guest
Posts: n/a
 
      06-27-2004
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.



 
Reply With Quote
 
 
 
 
DalePres
Guest
Posts: n/a
 
      06-27-2004
Make sure you add a declaration for the control in your code-behind class.

Dale

"William Parker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>
>



 
Reply With Quote
 
 
 
 
William Parker
Guest
Posts: n/a
 
      06-28-2004
Thanks Dale, but how *exactly* do I go about doing that?

For example in my original post I said this:
"If I add a declaration above Page_Load in the code behind 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. So obviously this is the wrong approach, but the right idea?"

Anyway I am confused how exactly I am support to add a declaration for the
contorl in the code behind which references the user control created in the
..aspx with:
<%@ Register TagPrefix="uc1" TagName="header" Src="header.ascx" %>
<uc1:header Title="This is working!!" id="Header1"

Can you please advise? Thanks!


"DalePres" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Make sure you add a declaration for the control in your code-behind class.
>
> Dale
>




 
Reply With Quote
 
Saravana [MVP]
Guest
Posts: n/a
 
      06-28-2004
You shouldnt declare that component as system.web.ui.usercontrol in
codebehind, instead you should declare that component as your usercontrol
like <Usercontrolnamespace>.<usercontrolclassname>. So that you can access
your usercontrol properties inside codebehind

--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com



"William Parker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks Dale, but how *exactly* do I go about doing that?
>
> For example in my original post I said this:
> "If I add a declaration above Page_Load in the code behind 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. So obviously this is the wrong approach, but the right idea?"
>
> Anyway I am confused how exactly I am support to add a declaration for the
> contorl in the code behind which references the user control created in

the
> .aspx with:
> <%@ Register TagPrefix="uc1" TagName="header" Src="header.ascx" %>
> <uc1:header Title="This is working!!" id="Header1"
>
> Can you please advise? Thanks!
>
>
> "DalePres" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Make sure you add a declaration for the control in your code-behind

class.
> >
> > Dale
> >

>
>
>



 
Reply With Quote
 
Jeffrey Palermo [MCP]
Guest
Posts: n/a
 
      06-28-2004
William,
protected YourNamespace.header Header1;

and then you can access Header1.Title.

You have named your usercontrol with a Type name, and then you access it
through a variable name, so if I have a user control that I have created
<uc1:Header ID="myHeader" runat="server"/>
then in my code behind, I need to declare it:
protected MyNamespace.Header myHeader;

now I can say: myHeader.Title. . .
but if you declare it as UserControl, then you can only access properties of
UserControl, so you need to declare it as the correct custom type.

Best regards,
Jeffrey Palermo

"William Parker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>
>



 
Reply With Quote
 
William Parker
Guest
Posts: n/a
 
      06-28-2004
Thank you - this is exactly what I was not understanding, and with this help
it is now working great!!

"Jeffrey Palermo [MCP]" <http://dotnetjunkies.com/weblog/jpalermo> wrote in
message news:(E-Mail Removed)...
> William,
> protected YourNamespace.header Header1;
>
> and then you can access Header1.Title.
>
> You have named your usercontrol with a Type name, and then you access it
> through a variable name, so if I have a user control that I have created
> <uc1:Header ID="myHeader" runat="server"/>
> then in my code behind, I need to declare it:
> protected MyNamespace.Header myHeader;
>
> now I can say: myHeader.Title. . .
> but if you declare it as UserControl, then you can only access properties

of
> UserControl, so you need to declare it as the correct custom type.
>
> Best regards,
> Jeffrey Palermo
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
C#/ASP.NET code behind access the objects in Web Control on the fo JB ASP .Net 3 03-05-2009 11:26 AM
Non-code behind to code behind John ASP .Net 2 02-19-2007 07:08 PM
How to access web control from code behind page? William Parker ASP .Net 5 06-28-2004 04:39 AM
Re: Code Behind vs. no code behind: error Ben Miller [msft] ASP .Net 1 06-28-2003 01:46 AM



Advertisments