Hi,
I am a web page and a web user control. My web user control is placed in my
web page using the following directive
<%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx"
%>
The web user control contains the following server controls
<asp:Literal id="pageTitle" runat="server"
EnableViewState="true"></asp:Literal>
<asp:Literal id="styleheets" runat="server"
EnableViewState="true"></asp:Literal>
<asp:Literal id="Javascipts" runat="server"
EnableViewState="true"></asp:Literal>
Can anybody please tell me how can reference the above user control
variables from the code behind page of my web form (not the code behind page
of my user control)
The trouble seems to be that there is no declaration of my user control in
my web form. for example the <@register ..> directive does not have an id
element that is reference in the webform.
thank you in advance.
any help is greatly appreciated.
cheers
martin. 6 11104
When you stick your user control in your web form, you
should give it an id (the id attribute is built-into the
System.Web.UI.UserControl class, which your Header user
control class is inheriting from), for example:
<html>
<body>
...
<uc1:Header id="headerUC" runat="server"></uc1:Header>
...
</body>
</html>
And then you should be able to reference that user control
in the code behind of the web form using the id you
assigned it (headerUC) and the user control class name
(which i'm asuuming is Header because of the Src
field "WebControls/Header.ascx"), like so:
Protected headerUC as Header
Here's the link to the documentation: http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpref/html/frlrfsystemwebuiusercontrolmemberstopic.asp
Hope this helps :) -----Original Message----- Hi,
I am a web page and a web user control. My web user
control is placed in myweb page using the following directive
<%@ Register TagPrefix="uc1" TagName="Header"
Src="WebControls/Header.ascx"%>
The web user control contains the following server
controls <asp:Literal id="pageTitle" runat="server" EnableViewState="true"></asp:Literal> <asp:Literal id="styleheets" runat="server" EnableViewState="true"></asp:Literal> <asp:Literal id="Javascipts" runat="server" EnableViewState="true"></asp:Literal>
Can anybody please tell me how can reference the above
user controlvariables from the code behind page of my web form (not
the code behind pageof my user control)
The trouble seems to be that there is no declaration of
my user control inmy web form. for example the <@register ..> directive
does not have an idelement that is reference in the webform.
thank you in advance.
any help is greatly appreciated.
cheers
martin.
.
Hi Dune,
Thanks for that.
I have taken your advice and got a bit further along the track
The following declaration is in my .aspx file
<%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx"
%>
<uc1:Header id="Header1" runat="server"></uc1:Header> --- so I need to
reference a variable called "pageTitle" in the header control
"pageTitle" is declared in the .ascx file like so
<asp:Literal id="pageTitle" runat="server"
EnableViewState="true"></asp:Literal>
and in the ascx.vb file like so
public WithEvents pageTitle As System.Web.UI.WebControls.Literal
my problem now is that I want to change the text of "pageTitle" from my
..aspx.vb file i.e the code behind of the web page not the code behind of
the control.
so I need an instance of the user control in the code behind of the webpage.
I add this lke so
This is the line I could have got wrong.
Public myControl As Control =
CType(Page.LoadControl("WebControls/Header.ascx"), Control)
so know I am thinking I can change the value of the variable in the user
control from the web page like so
myControl .pageTile() = "welcome to my web page"
but it doesn't work. Perhaps it is something to do with the declaration of
the usercontrol being of type control and then cast to the actual user
control rather than declaring an instance of the user control straight away.
cheers
martin.
"Dune" <an*******@discussions.microsoft.com> wrote in message
news:03****************************@phx.gbl... When you stick your user control in your web form, you should give it an id (the id attribute is built-into the System.Web.UI.UserControl class, which your Header user control class is inheriting from), for example:
<html> <body> ...
<uc1:Header id="headerUC" runat="server"></uc1:Header>
... </body> </html>
And then you should be able to reference that user control in the code behind of the web form using the id you assigned it (headerUC) and the user control class name (which i'm asuuming is Header because of the Src field "WebControls/Header.ascx"), like so:
Protected headerUC as Header
Here's the link to the documentation:
http://msdn.microsoft.com/library/default.asp? url=/library/en- us/cpref/html/frlrfsystemwebuiusercontrolmemberstopic.asp
Hope this helps :) -----Original Message----- Hi,
I am a web page and a web user control. My web user control is placed in myweb page using the following directive
<%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx"%>
The web user control contains the following server controls <asp:Literal id="pageTitle" runat="server" EnableViewState="true"></asp:Literal> <asp:Literal id="styleheets" runat="server" EnableViewState="true"></asp:Literal> <asp:Literal id="Javascipts" runat="server" EnableViewState="true"></asp:Literal>
Can anybody please tell me how can reference the above
user controlvariables from the code behind page of my web form (not the code behind pageof my user control)
The trouble seems to be that there is no declaration of my user control inmy web form. for example the <@register ..> directive does not have an idelement that is reference in the webform.
thank you in advance.
any help is greatly appreciated.
cheers
martin.
.
i found it easier to reply under what you have already
written so i could refer to specific bits...so scroll down
to see my reply.
I've marked my replies with "Dune Says:" and i've marked
code examples with "Dune Start Code Example:" and "Dune
End Code Example". -----Original Message----- Hi Dune,
Thanks for that. I have taken your advice and got a bit further along the
trackThe following declaration is in my .aspx file
<%@ Register TagPrefix="uc1" TagName="Header"
Src="WebControls/Header.ascx"%> <uc1:Header id="Header1" runat="server"></uc1:Header> -
-- so I need toreference a variable called "pageTitle" in the header
control "pageTitle" is declared in the .ascx file like so
<asp:Literal id="pageTitle" runat="server" EnableViewState="true"></asp:Literal> and in the ascx.vb file like so public WithEvents pageTitle As
System.Web.UI.WebControls.Literal my problem now is that I want to change the text
of "pageTitle" from my..aspx.vb file i.e the code behind of the web page not
the code behind ofthe control.
so I need an instance of the user control in the code
behind of the webpage.I add this lke so
This is the line I could have got wrong.
Public myControl As Control = CType(Page.LoadControl("WebControls/Header.ascx"),
Control)
Dune Says:
Ok, the line above should be:
Dune Start Code Example:
Protected Header1 As Header
Dune End Code Example
Dune Says:
and that's all. you don't need to load the control because
you have already put it in the html of your web form (you
only use the LoadControl syntax when you want to create
user controls PROGRAMATICALLY in your code-behind).
Also, be sure to match the name of the variable you're
declaring in the code-behind to the id you gave that
object in the html. Plus, be sure that you match the type
of the variable you're declaring to the correct class it
belongs to.
so know I am thinking I can change the value of the
variable in the usercontrol from the web page like so
myControl .pageTile() = "welcome to my web page"
but it doesn't work. Perhaps it is something to do with
the declaration ofthe usercontrol being of type control and then cast to
the actual usercontrol rather than declaring an instance of the user
control straight away.
Dune Says:
You can't access the variables declared in your user
control because they are private (or protected) to your
user control class. In order to access those variables,
you must set them up as PROPERTIES of your user control
class.
Dune Says:
So, the code behind for your user control class should
look something like this:
Dune Start Code Example:
Public MustInherit Class Header
Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As
System.Web.UI.WebControls.Literal
#Region " Web Form Designer Generated Code "
...
#End Region
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
...
End Sub
End Class
Dune End Code Example
Dune Says:
Note that in the code-behind, you must declare the literal
control (pageTitle) that you have put in your html using
the line:
Dune Start Code Example:
Protected WithEvents pageTitle As
System.Web.UI.WebControls.Literal
Dune End Code Example
Dune Says:
Ok, now you must add a new property so you can reference
the pageTitle literal control, here is what the code-
behind of your user control should look like with the new
property:
Dune Start Code Example:
Public MustInherit Class Header
Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As
System.Web.UI.WebControls.Literal
Public Property PageTitle() As String
Get
Return pageTitle.Text
End Get
Set(ByVal Value As String)
pageTitle.Text = Value
End Set
End Property
#Region " Web Form Designer Generated Code "
...
#End Region
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
...
End Sub
End Class
Dune End Code Example
Dune Says:
Right, now you should have everything you need. So, in the
code-behind of your web form you can now say:
Dune Start Code Example:
Header1.PageTitle = "blah"
Dune End Code Example
Dune Says:
And that should all work.
More info on Properties: http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/vblr7/html/vborivblangreftopnode.asp
More info on User Controls in general: http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconwebformsusercontrols.asp
cheers
martin.
"Dune" <an*******@discussions.microsoft.com> wrote in
messagenews:03****************************@phx.gbl... When you stick your user control in your web form, you should give it an id (the id attribute is built-into the System.Web.UI.UserControl class, which your Header user control class is inheriting from), for example:
<html> <body> ...
<uc1:Header id="headerUC" runat="server"></uc1:Header>
... </body> </html>
And then you should be able to reference that user
control in the code behind of the web form using the id you assigned it (headerUC) and the user control class name (which i'm asuuming is Header because of the Src field "WebControls/Header.ascx"), like so:
Protected headerUC as Header
Here's the link to the documentation:
http://msdn.microsoft.com/library/default.asp? url=/library/en-
us/cpref/html/frlrfsystemwebuiusercontrolmemberstopic.asp Hope this helps :) >-----Original Message----- >Hi, > >I am a web page and a web user control. My web user control is placed in my >web page using the following directive > ><%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" >%> > >The web user control contains the following server controls > ><asp:Literal id="pageTitle" runat="server" >EnableViewState="true"></asp:Literal> ><asp:Literal id="styleheets" runat="server" >EnableViewState="true"></asp:Literal> ><asp:Literal id="Javascipts" runat="server" >EnableViewState="true"></asp:Literal> > >Can anybody please tell me how can reference the above user control >variables from the code behind page of my web form (not the code behind page >of my user control) > >The trouble seems to be that there is no declaration of my user control in >my web form. for example the <@register ..> directive does not have an id >element that is reference in the webform. > >thank you in advance. > >any help is greatly appreciated. > >cheers > >martin. > > >. >
.
Hi dune,
well thanks for that. I tried your appraoch and it worked perfectly.
there is just one flaw. The page_load event of my web page (.aspx.vb) fires
before the page load event of my webcontrol (ascx.vb)
I was hoping that i could set the variable to a "default" value inside the
web control and then over ride it with another value if needed in the actual
webpage itself.
seems this isn't possible as the web control will fire after the webpage has
loaded and thus over write any value set in the webpage.
I guess I'll just have to check if the value has been set in the webcontrol
before attemping to over write it.
anyway, thanks for your help.
cheers
martin.
"Dune" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl... i found it easier to reply under what you have already written so i could refer to specific bits...so scroll down to see my reply.
I've marked my replies with "Dune Says:" and i've marked code examples with "Dune Start Code Example:" and "Dune End Code Example".
-----Original Message----- Hi Dune,
Thanks for that. I have taken your advice and got a bit further along the trackThe following declaration is in my .aspx file
<%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx"%> <uc1:Header id="Header1" runat="server"></uc1:Header> - -- so I need toreference a variable called "pageTitle" in the header control "pageTitle" is declared in the .ascx file like so
<asp:Literal id="pageTitle" runat="server" EnableViewState="true"></asp:Literal> and in the ascx.vb file like so public WithEvents pageTitle As
System.Web.UI.WebControls.Literal my problem now is that I want to change the text
of "pageTitle" from my..aspx.vb file i.e the code behind of the web page not the code behind ofthe control.
so I need an instance of the user control in the code behind of the webpage.I add this lke so
This is the line I could have got wrong.
Public myControl As Control = CType(Page.LoadControl("WebControls/Header.ascx"), Control)
Dune Says: Ok, the line above should be:
Dune Start Code Example:
Protected Header1 As Header
Dune End Code Example
Dune Says: and that's all. you don't need to load the control because you have already put it in the html of your web form (you only use the LoadControl syntax when you want to create user controls PROGRAMATICALLY in your code-behind). Also, be sure to match the name of the variable you're declaring in the code-behind to the id you gave that object in the html. Plus, be sure that you match the type of the variable you're declaring to the correct class it belongs to.
so know I am thinking I can change the value of the variable in the usercontrol from the web page like so
myControl .pageTile() = "welcome to my web page"
but it doesn't work. Perhaps it is something to do with the declaration ofthe usercontrol being of type control and then cast to the actual usercontrol rather than declaring an instance of the user control straight away. Dune Says: You can't access the variables declared in your user control because they are private (or protected) to your user control class. In order to access those variables, you must set them up as PROPERTIES of your user control class.
Dune Says: So, the code behind for your user control class should look something like this:
Dune Start Code Example:
Public MustInherit Class Header Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
#Region " Web Form Designer Generated Code " ... #End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ... End Sub
End Class
Dune End Code Example
Dune Says: Note that in the code-behind, you must declare the literal control (pageTitle) that you have put in your html using the line:
Dune Start Code Example:
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
Dune End Code Example
Dune Says: Ok, now you must add a new property so you can reference the pageTitle literal control, here is what the code- behind of your user control should look like with the new property:
Dune Start Code Example:
Public MustInherit Class Header Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
Public Property PageTitle() As String Get Return pageTitle.Text End Get
Set(ByVal Value As String) pageTitle.Text = Value End Set End Property
#Region " Web Form Designer Generated Code " ... #End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ... End Sub
End Class
Dune End Code Example
Dune Says: Right, now you should have everything you need. So, in the code-behind of your web form you can now say:
Dune Start Code Example:
Header1.PageTitle = "blah"
Dune End Code Example
Dune Says: And that should all work.
More info on Properties:
http://msdn.microsoft.com/library/default.asp? url=/library/en-us/vblr7/html/vborivblangreftopnode.asp
More info on User Controls in general:
http://msdn.microsoft.com/library/default.asp? url=/library/en- us/cpguide/html/cpconwebformsusercontrols.asp
cheers
martin.
"Dune" <an*******@discussions.microsoft.com> wrote in messagenews:03****************************@phx.gbl... When you stick your user control in your web form, you should give it an id (the id attribute is built-into the System.Web.UI.UserControl class, which your Header user control class is inheriting from), for example:
<html> <body> ...
<uc1:Header id="headerUC" runat="server"></uc1:Header>
... </body> </html>
And then you should be able to reference that user control in the code behind of the web form using the id you assigned it (headerUC) and the user control class name (which i'm asuuming is Header because of the Src field "WebControls/Header.ascx"), like so:
Protected headerUC as Header
Here's the link to the documentation:
http://msdn.microsoft.com/library/default.asp? url=/library/en- us/cpref/html/frlrfsystemwebuiusercontrolmemberstopic.asp Hope this helps :) >-----Original Message----- >Hi, > >I am a web page and a web user control. My web user control is placed in my >web page using the following directive > ><%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" >%> > >The web user control contains the following server controls > ><asp:Literal id="pageTitle" runat="server" >EnableViewState="true"></asp:Literal> ><asp:Literal id="styleheets" runat="server" >EnableViewState="true"></asp:Literal> ><asp:Literal id="Javascipts" runat="server" >EnableViewState="true"></asp:Literal> > >Can anybody please tell me how can reference the above user control >variables from the code behind page of my web form (not the code behind page >of my user control) > >The trouble seems to be that there is no declaration of my user control in >my web form. for example the <@register ..> directive does not have an id >element that is reference in the webform. > >thank you in advance. > >any help is greatly appreciated. > >cheers > >martin. > > >. >
.
It's true that the web from page_load fires before the
user control page_load, but i don't think that that really
matters for what you want to achieve.
You can set a default value in the user control (.ascx)
page_load and then in the web form (.aspx) page_load do a
check for the condition that will cause the default to be
overwritten by using an if...else statement. That way, the
default value will always apply unless a certain condition
is met. So, in the web form (.aspx) page_load:
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
If userWantsNewTitle = True Then
Header1.PageTitle = "random title"
End If
End Sub
Also, just fyi...if you don't want to set a default that
will apply to every single instance of your header user
control (which is what will happen if you set the default
in the code-behind of the user control), you can actually
treat the PageTitle property like any another html tag
attribute in the web form html. So, in the web form hmtl,
where you stuck in your user control tag, you can say:
<html>
<body>
...
<uc1:header id="Header1" runat="server"
pagetitle="default title"></uc1:header>
...
</body>
</html>
This means that this PARTICULAR header user control
(distinguished by it's id of "Header1") for this web form
will always start out life with PageTitle set to "default
title" but any other header user controls will not
(regardless of whether they are in the same web form or
not). But of course, you will still need some sort of If
statement like the one shown earlier in the web form code-
behind in order to overrride the PageTitle value given a
certain condition. -----Original Message----- Hi dune,
well thanks for that. I tried your appraoch and it worked
perfectly. there is just one flaw. The page_load event of my web
page (.aspx.vb) firesbefore the page load event of my webcontrol (ascx.vb)
I was hoping that i could set the variable to a "default"
value inside theweb control and then over ride it with another value if
needed in the actualwebpage itself. seems this isn't possible as the web control will fire
after the webpage hasloaded and thus over write any value set in the webpage.
I guess I'll just have to check if the value has been set
in the webcontrolbefore attemping to over write it.
anyway, thanks for your help.
cheers
martin. "Dune" <an*******@discussions.microsoft.com> wrote in
messagenews:04****************************@phx.gbl... i found it easier to reply under what you have already written so i could refer to specific bits...so scroll
down to see my reply.
I've marked my replies with "Dune Says:" and i've marked code examples with "Dune Start Code Example:" and "Dune End Code Example".
>-----Original Message----- >Hi Dune, > >Thanks for that. >I have taken your advice and got a bit further along
the track >The following declaration is in my .aspx file > ><%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" >%> ><uc1:Header id="Header1"
runat="server"></uc1:Header> - -- so I need to >reference a variable called "pageTitle" in the header control > >"pageTitle" is declared in the .ascx file like so > ><asp:Literal id="pageTitle" runat="server" >EnableViewState="true"></asp:Literal> >and in the ascx.vb file like so >public WithEvents pageTitle As System.Web.UI.WebControls.Literal > >my problem now is that I want to change the text of "pageTitle" from my >..aspx.vb file i.e the code behind of the web page not the code behind of >the control. > >so I need an instance of the user control in the code behind of the webpage. >I add this lke so > >This is the line I could have got wrong. > >Public myControl As Control = >CType(Page.LoadControl("WebControls/Header.ascx"), Control) >
Dune Says: Ok, the line above should be:
Dune Start Code Example:
Protected Header1 As Header
Dune End Code Example
Dune Says: and that's all. you don't need to load the control
because you have already put it in the html of your web form
(you only use the LoadControl syntax when you want to create user controls PROGRAMATICALLY in your code-behind). Also, be sure to match the name of the variable you're declaring in the code-behind to the id you gave that object in the html. Plus, be sure that you match the
type of the variable you're declaring to the correct class it belongs to.
>so know I am thinking I can change the value of the variable in the user >control from the web page like so > >myControl .pageTile() = "welcome to my web page" > >but it doesn't work. Perhaps it is something to do with the declaration of >the usercontrol being of type control and then cast to the actual user >control rather than declaring an instance of the user control straight away. > Dune Says: You can't access the variables declared in your user control because they are private (or protected) to your user control class. In order to access those variables, you must set them up as PROPERTIES of your user control class.
Dune Says: So, the code behind for your user control class should look something like this:
Dune Start Code Example:
Public MustInherit Class Header Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
#Region " Web Form Designer Generated Code " ... #End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ... End Sub
End Class
Dune End Code Example
Dune Says: Note that in the code-behind, you must declare the
literal control (pageTitle) that you have put in your html using the line:
Dune Start Code Example:
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
Dune End Code Example
Dune Says: Ok, now you must add a new property so you can reference the pageTitle literal control, here is what the code- behind of your user control should look like with the
new property:
Dune Start Code Example:
Public MustInherit Class Header Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
Public Property PageTitle() As String Get Return pageTitle.Text End Get
Set(ByVal Value As String) pageTitle.Text = Value End Set End Property
#Region " Web Form Designer Generated Code " ... #End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ... End Sub
End Class
Dune End Code Example
Dune Says: Right, now you should have everything you need. So, in
the code-behind of your web form you can now say:
Dune Start Code Example:
Header1.PageTitle = "blah"
Dune End Code Example
Dune Says: And that should all work.
More info on Properties:
http://msdn.microsoft.com/library/default.asp? url=/library/en-us/vblr7/html/vborivblangreftopnode.asp
More info on User Controls in general:
http://msdn.microsoft.com/library/default.asp? url=/library/en- us/cpguide/html/cpconwebformsusercontrols.asp
>cheers > >martin. > > > > > > > > > > > > > > >"Dune" <an*******@discussions.microsoft.com> wrote in message >news:03****************************@phx.gbl... >> When you stick your user control in your web form,
you >> should give it an id (the id attribute is built-into
the >> System.Web.UI.UserControl class, which your Header
user >> control class is inheriting from), for example: >> >> <html> >> <body> >> ... >> >> <uc1:Header id="headerUC"
runat="server"></uc1:Header> >> >> ... >> </body> >> </html> >> >> And then you should be able to reference that user control >> in the code behind of the web form using the id you >> assigned it (headerUC) and the user control class
name >> (which i'm asuuming is Header because of the Src >> field "WebControls/Header.ascx"), like so: >> >> Protected headerUC as Header >> >> >> Here's the link to the documentation: >> >> http://msdn.microsoft.com/library/default.asp? >> url=/library/en- >>
us/cpref/html/frlrfsystemwebuiusercontrolmemberstopic.asp >> >> Hope this helps :) >> >> >> >> >> >> >> >-----Original Message----- >> >Hi, >> > >> >I am a web page and a web user control. My web user >> control is placed in my >> >web page using the following directive >> > >> ><%@ Register TagPrefix="uc1" TagName="Header" >> Src="WebControls/Header.ascx" >> >%> >> > >> >The web user control contains the following server >> controls >> > >> ><asp:Literal id="pageTitle" runat="server" >> >EnableViewState="true"></asp:Literal> >> ><asp:Literal id="styleheets" runat="server" >> >EnableViewState="true"></asp:Literal> >> ><asp:Literal id="Javascipts" runat="server" >> >EnableViewState="true"></asp:Literal> >> > >> >Can anybody please tell me how can reference the
above >> user control >> >variables from the code behind page of my web form
(not >> the code behind page >> >of my user control) >> > >> >The trouble seems to be that there is no
declaration of >> my user control in >> >my web form. for example the <@register ..>
directive >> does not have an id >> >element that is reference in the webform. >> > >> >thank you in advance. >> > >> >any help is greatly appreciated. >> > >> >cheers >> > >> >martin. >> > >> > >> >. >> > > > >. >
.
Ok,
That all cool.
passing through an attribute to the user control like so
<html>
<body>
...
<uc1:header id="Header1" runat="server"
pagetitle="default title"></uc1:header>
...
</body>
</html>
makes perfect sense.
Thanks again for you help.
cheers
martin.
"Dune" <an*******@discussions.microsoft.com> wrote in message
news:07****************************@phx.gbl... It's true that the web from page_load fires before the user control page_load, but i don't think that that really matters for what you want to achieve.
You can set a default value in the user control (.ascx) page_load and then in the web form (.aspx) page_load do a check for the condition that will cause the default to be overwritten by using an if...else statement. That way, the default value will always apply unless a certain condition is met. So, in the web form (.aspx) page_load:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If userWantsNewTitle = True Then Header1.PageTitle = "random title" End If End Sub
Also, just fyi...if you don't want to set a default that will apply to every single instance of your header user control (which is what will happen if you set the default in the code-behind of the user control), you can actually treat the PageTitle property like any another html tag attribute in the web form html. So, in the web form hmtl, where you stuck in your user control tag, you can say:
<html> <body> ... <uc1:header id="Header1" runat="server" pagetitle="default title"></uc1:header> ... </body> </html>
This means that this PARTICULAR header user control (distinguished by it's id of "Header1") for this web form will always start out life with PageTitle set to "default title" but any other header user controls will not (regardless of whether they are in the same web form or not). But of course, you will still need some sort of If statement like the one shown earlier in the web form code- behind in order to overrride the PageTitle value given a certain condition.
-----Original Message----- Hi dune,
well thanks for that. I tried your appraoch and it worked perfectly. there is just one flaw. The page_load event of my web
page (.aspx.vb) firesbefore the page load event of my webcontrol (ascx.vb)
I was hoping that i could set the variable to a "default" value inside theweb control and then over ride it with another value if needed in the actualwebpage itself. seems this isn't possible as the web control will fire after the webpage hasloaded and thus over write any value set in the webpage.
I guess I'll just have to check if the value has been set in the webcontrolbefore attemping to over write it.
anyway, thanks for your help.
cheers
martin. "Dune" <an*******@discussions.microsoft.com> wrote in messagenews:04****************************@phx.gbl... i found it easier to reply under what you have already written so i could refer to specific bits...so scroll down to see my reply.
I've marked my replies with "Dune Says:" and i've marked code examples with "Dune Start Code Example:" and "Dune End Code Example".
>-----Original Message----- >Hi Dune, > >Thanks for that. >I have taken your advice and got a bit further along the track >The following declaration is in my .aspx file > ><%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx" >%> ><uc1:Header id="Header1" runat="server"></uc1:Header> - -- so I need to >reference a variable called "pageTitle" in the header control > >"pageTitle" is declared in the .ascx file like so > ><asp:Literal id="pageTitle" runat="server" >EnableViewState="true"></asp:Literal> >and in the ascx.vb file like so >public WithEvents pageTitle As System.Web.UI.WebControls.Literal > >my problem now is that I want to change the text of "pageTitle" from my >..aspx.vb file i.e the code behind of the web page not the code behind of >the control. > >so I need an instance of the user control in the code behind of the webpage. >I add this lke so > >This is the line I could have got wrong. > >Public myControl As Control = >CType(Page.LoadControl("WebControls/Header.ascx"), Control) >
Dune Says: Ok, the line above should be:
Dune Start Code Example:
Protected Header1 As Header
Dune End Code Example
Dune Says: and that's all. you don't need to load the control because you have already put it in the html of your web form (you only use the LoadControl syntax when you want to create user controls PROGRAMATICALLY in your code-behind). Also, be sure to match the name of the variable you're declaring in the code-behind to the id you gave that object in the html. Plus, be sure that you match the type of the variable you're declaring to the correct class it belongs to.
>so know I am thinking I can change the value of the variable in the user >control from the web page like so > >myControl .pageTile() = "welcome to my web page" > >but it doesn't work. Perhaps it is something to do with the declaration of >the usercontrol being of type control and then cast to the actual user >control rather than declaring an instance of the user control straight away. > Dune Says: You can't access the variables declared in your user control because they are private (or protected) to your user control class. In order to access those variables, you must set them up as PROPERTIES of your user control class.
Dune Says: So, the code behind for your user control class should look something like this:
Dune Start Code Example:
Public MustInherit Class Header Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
#Region " Web Form Designer Generated Code " ... #End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ... End Sub
End Class
Dune End Code Example
Dune Says: Note that in the code-behind, you must declare the literal control (pageTitle) that you have put in your html using the line:
Dune Start Code Example:
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
Dune End Code Example
Dune Says: Ok, now you must add a new property so you can reference the pageTitle literal control, here is what the code- behind of your user control should look like with the new property:
Dune Start Code Example:
Public MustInherit Class Header Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As System.Web.UI.WebControls.Literal
Public Property PageTitle() As String Get Return pageTitle.Text End Get
Set(ByVal Value As String) pageTitle.Text = Value End Set End Property
#Region " Web Form Designer Generated Code " ... #End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ... End Sub
End Class
Dune End Code Example
Dune Says: Right, now you should have everything you need. So, in the code-behind of your web form you can now say:
Dune Start Code Example:
Header1.PageTitle = "blah"
Dune End Code Example
Dune Says: And that should all work.
More info on Properties:
http://msdn.microsoft.com/library/default.asp? url=/library/en-us/vblr7/html/vborivblangreftopnode.asp
More info on User Controls in general:
http://msdn.microsoft.com/library/default.asp? url=/library/en- us/cpguide/html/cpconwebformsusercontrols.asp
>cheers > >martin. > > > > > > > > > > > > > > >"Dune" <an*******@discussions.microsoft.com> wrote in message >news:03****************************@phx.gbl... >> When you stick your user control in your web form, you >> should give it an id (the id attribute is built-into the >> System.Web.UI.UserControl class, which your Header user >> control class is inheriting from), for example: >> >> <html> >> <body> >> ... >> >> <uc1:Header id="headerUC" runat="server"></uc1:Header> >> >> ... >> </body> >> </html> >> >> And then you should be able to reference that user control >> in the code behind of the web form using the id you >> assigned it (headerUC) and the user control class name >> (which i'm asuuming is Header because of the Src >> field "WebControls/Header.ascx"), like so: >> >> Protected headerUC as Header >> >> >> Here's the link to the documentation: >> >> http://msdn.microsoft.com/library/default.asp? >> url=/library/en- >> us/cpref/html/frlrfsystemwebuiusercontrolmemberstopic.asp >> >> Hope this helps :) >> >> >> >> >> >> >> >-----Original Message----- >> >Hi, >> > >> >I am a web page and a web user control. My web user >> control is placed in my >> >web page using the following directive >> > >> ><%@ Register TagPrefix="uc1" TagName="Header" >> Src="WebControls/Header.ascx" >> >%> >> > >> >The web user control contains the following server >> controls >> > >> ><asp:Literal id="pageTitle" runat="server" >> >EnableViewState="true"></asp:Literal> >> ><asp:Literal id="styleheets" runat="server" >> >EnableViewState="true"></asp:Literal> >> ><asp:Literal id="Javascipts" runat="server" >> >EnableViewState="true"></asp:Literal> >> > >> >Can anybody please tell me how can reference the above >> user control >> >variables from the code behind page of my web form (not >> the code behind page >> >of my user control) >> > >> >The trouble seems to be that there is no declaration of >> my user control in >> >my web form. for example the <@register ..> directive >> does not have an id >> >element that is reference in the webform. >> > >> >thank you in advance. >> > >> >any help is greatly appreciated. >> > >> >cheers >> > >> >martin. >> > >> > >> >. >> > > > >. >
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Michel |
last post by:
Hi all,
Maybe a stupid question, but I wondered if it was possible to access one of
the public variables on the page from within a user control...
|
by: Owin |
last post by:
Hi all,
I've created an User control. It's an extension of a textbox wich has some extra properties so that validation becomes a lot faster.
...
|
by: owingruters |
last post by:
Hi all,
I've created an User control. It's an extension of a textbox wich has
some extra properties so that validation becomes a lot faster.
...
|
by: syedsheeraz |
last post by:
Hi all. Does anyone know if there is a way to gain programmatic access
to the user control inside of a page after it has been cached? Let me...
|
by: Matt Jensen |
last post by:
Howdy all
Been searching and can't find a good answer to my problem.
I've got a usercontrol 'banner' at the top of my page, and when the...
|
by: ivanpais |
last post by:
Hi,
I have a Web User Control, Lets say "Foo.ascx", that contains a button
"btnFoo".
I have a Master Page "Bar.master", that has a label...
|
by: CodeRazor |
last post by:
I have a user contol hosted in a webform. The user control interfaces with an
http component.
When the user control loads, a request is sent to the...
|
by: AlecL |
last post by:
I have a page that has about 5 or 6 user controls ranging from hearders
to footers to ad space but when I add a new user control which is a
form it...
|
by: =?Utf-8?B?VDhS?= |
last post by:
Ok...I've been looking for an answer to this problem and can't seem to find
one...Framework 1.1 mind you.
I have a base class that inherits from...
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
| |