473,700 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Referencing user control variables from page

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="WebControl s/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
6 11285
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.U serControl 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 "WebControl s/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/frlrfsystemwebu iusercontrolmem berstopic.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="WebControl s/Header.ascx"%>

The web user control contains the following server controls
<asp:Literal id="pageTitle" runat="server"
EnableViewStat e="true"></asp:Literal>
<asp:Literal id="styleheets " runat="server"
EnableViewStat e="true"></asp:Literal>
<asp:Literal id="Javascipts " runat="server"
EnableViewStat e="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
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="WebControl s/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.W ebControls.Lite ral

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.Load Control("WebCon trols/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*******@disc ussions.microso ft.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.U serControl 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 "WebControl s/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/frlrfsystemwebu iusercontrolmem berstopic.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="WebControl s/Header.ascx"
%>

The web user control contains the following server

controls

<asp:Literal id="pageTitle" runat="server"
EnableViewStat e="true"></asp:Literal>
<asp:Literal id="styleheets " runat="server"
EnableViewStat e="true"></asp:Literal>
<asp:Literal id="Javascipts " runat="server"
EnableViewStat e="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
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="WebControl s/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"
EnableViewStat e="true"></asp:Literal>
and in the ascx.vb file like so
public WithEvents pageTitle As System.Web.UI.W ebControls.Lite ral
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.Loa dControl("WebCo ntrols/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.U serControl

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

#Region " Web Form Designer Generated Code "
...
#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArg s) 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.W ebControls.Lite ral

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

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

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.EventArg s) 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.PageTit le = "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/vborivblangreft opnode.asp

More info on User Controls in general:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconwebformsus ercontrols.asp
cheers

martin.




"Dune" <an*******@disc ussions.microso ft.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.U serControl 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 "WebControl s/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/frlrfsystemwebu iusercontrolmem berstopic.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="WebControl s/Header.ascx"
>%>
>
>The web user control contains the following server

controls
>
><asp:Literal id="pageTitle" runat="server"
>EnableViewStat e="true"></asp:Literal>
><asp:Literal id="styleheets " runat="server"
>EnableViewStat e="true"></asp:Literal>
><asp:Literal id="Javascipts " runat="server"
>EnableViewStat e="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
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*******@disc ussions.microso ft.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="WebControl s/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"
EnableViewStat e="true"></asp:Literal>
and in the ascx.vb file like so
public WithEvents pageTitle As

System.Web.UI.W ebControls.Lite ral

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.Loa dControl("WebCo ntrols/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.U serControl

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

#Region " Web Form Designer Generated Code "
...
#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArg s) 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.W ebControls.Lite ral

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

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

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.EventArg s) 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.PageTit le = "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/vborivblangreft opnode.asp

More info on User Controls in general:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconwebformsus ercontrols.asp
cheers

martin.




"Dune" <an*******@disc ussions.microso ft.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.U serControl 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 "WebControl s/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/frlrfsystemwebu iusercontrolmem berstopic.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="WebControl s/Header.ascx"
>%>
>
>The web user control contains the following server
controls
>
><asp:Literal id="pageTitle" runat="server"
>EnableViewStat e="true"></asp:Literal>
><asp:Literal id="styleheets " runat="server"
>EnableViewStat e="true"></asp:Literal>
><asp:Literal id="Javascipts " runat="server"
>EnableViewStat e="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
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.EventArg s) Handles MyBase.Load
If userWantsNewTit le = True Then
Header1.PageTit le = "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="defa ult 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*******@disc ussions.microso ft.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="WebControl s/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"
>EnableViewStat e="true"></asp:Literal>
>and in the ascx.vb file like so
>public WithEvents pageTitle As

System.Web.UI.W ebControls.Lite ral
>
>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.Loa dControl("WebCo ntrols/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.U serControl

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

#Region " Web Form Designer Generated Code "
...
#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArg s) 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.W ebControls.Lite ral

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

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

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.EventArg s) 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.PageTit le = "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/vborivblangreft opnode.asp

More info on User Controls in general:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconwebformsus ercontrols.asp
>cheers
>
>martin.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>"Dune" <an*******@disc ussions.microso ft.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.U serControl 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 "WebControl s/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/frlrfsystemwebu iusercontrolmem berstopic.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="WebControl s/Header.ascx"
>> >%>
>> >
>> >The web user control contains the following server
>> controls
>> >
>> ><asp:Literal id="pageTitle" runat="server"
>> >EnableViewStat e="true"></asp:Literal>
>> ><asp:Literal id="styleheets " runat="server"
>> >EnableViewStat e="true"></asp:Literal>
>> ><asp:Literal id="Javascipts " runat="server"
>> >EnableViewStat e="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
Ok,

That all cool.
passing through an attribute to the user control like so

<html>
<body>
...
<uc1:header id="Header1" runat="server"
pagetitle="defa ult title"></uc1:header>
...
</body>
</html>
makes perfect sense.

Thanks again for you help.

cheers

martin.


"Dune" <an*******@disc ussions.microso ft.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.EventArg s) Handles MyBase.Load
If userWantsNewTit le = True Then
Header1.PageTit le = "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="defa ult 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*******@disc ussions.microso ft.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="WebControl s/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"
>EnableViewStat e="true"></asp:Literal>
>and in the ascx.vb file like so
>public WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral
>
>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.Loa dControl("WebCo ntrols/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.U serControl

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

#Region " Web Form Designer Generated Code "
...
#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArg s) 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.W ebControls.Lite ral

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

Protected WithEvents pageTitle As
System.Web.UI.W ebControls.Lite ral

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.EventArg s) 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.PageTit le = "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/vborivblangreft opnode.asp

More info on User Controls in general:

http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconwebformsus ercontrols.asp

>cheers
>
>martin.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>"Dune" <an*******@disc ussions.microso ft.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.U serControl 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 "WebControl s/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/frlrfsystemwebu iusercontrolmem berstopic.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="WebControl s/Header.ascx"
>> >%>
>> >
>> >The web user control contains the following server
>> controls
>> >
>> ><asp:Literal id="pageTitle" runat="server"
>> >EnableViewStat e="true"></asp:Literal>
>> ><asp:Literal id="styleheets " runat="server"
>> >EnableViewStat e="true"></asp:Literal>
>> ><asp:Literal id="Javascipts " runat="server"
>> >EnableViewStat e="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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1446
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 on that page? I would like to use a property in the user control to access the correct public variable. This property should contain the variables name. Thanks in advance,
2
3970
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. The control wordks great if autopostback is on. When there's a value in the textbox, the (overridden) textchanged event fires and validation is performed. Now I want to turn off autopostback. So I want my contol only validate if the submit button is pressed. I've tried Page.Validate(), but It seems...
4
15686
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. The control wordks great if autopostback is on. When there's a value in the textbox, the (overridden) textchanged event fires and validation is performed. Now I want to turn off autopostback. So I want my contol only validate
0
1249
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 elaborate more by telling you what I am doing. I am using page fragment caching to cache results of a search while the header is dynamically loaded each time. So I have a page containing a user control which contains my datagrid for displaying the results. The user control has the outputcache...
4
1884
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 selection in the dropdown list changes I want the "host" webform/page to be reload so the data on it (in repeaters) is reloaded using a new value from the dropdownlist (and which btw is also stored in a session variable for later use). I'm not sure the best way to do this.
3
6431
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 "lblBar". This label is exposed by a public property BarLabelText. I now have a contentpage "FooBar.aspx", where "Bar.master" is the master page and in the content section has the control "Foo.ascx".
3
3176
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 http component. It takes around 10 seconds before a response is returned. While this is happening the webform cannot do any other processing. I want to use a client callback to free up the web form while the usercontrol is busy doing its stuff. I have got it working fine if I generate the...
2
1876
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 gives me the exception "page can have only one server-side Form tag" error. Here is the brief code that I am adding to the code: on top: <%@ Register TagPrefix="uc" TagName="CareerStats" Src="/Controls/CareerStats.ascx" %> and the div where I added the control:
3
1439
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 UserControl. I have 5 and soon to be 12 user controls that derive from this base class that represent US States with appropriate business logic and rules. Inside the base class is an event that is raised whenever you click a checkbox in the datagrid of each of the user controls...no biggie... ...
0
8713
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9203
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7795
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6556
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2021
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.