By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Referencing user control variables from page

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
.

Nov 18 '05 #2

P: n/a
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 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.
.

Nov 18 '05 #3

P: n/a
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.
>
>
>.
>

.

Nov 18 '05 #4

P: n/a
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

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.
>
>
>.
>

.

Nov 18 '05 #5

P: n/a
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.
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 18 '05 #6

P: n/a
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) 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 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.
>> >
>> >
>> >.
>> >
>
>
>.
>

.

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.