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" <anonymous@discussions.microsoft.com> wrote in message
news:073701c3d647$4d66f140$a101280a@phx.gbl...[color=blue]
> 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.
>[color=green]
> >-----Original Message-----
> >Hi dune,
> >
> >well thanks for that. I tried your appraoch and it worked[/color]
> perfectly.[color=green]
> >
> >there is just one flaw. The page_load event of my web[/color]
> page (.aspx.vb) fires[color=green]
> >before the page load event of my webcontrol (ascx.vb)
> >
> >I was hoping that i could set the variable to a "default"[/color]
> value inside the[color=green]
> >web control and then over ride it with another value if[/color]
> needed in the actual[color=green]
> >webpage itself.
> >seems this isn't possible as the web control will fire[/color]
> after the webpage has[color=green]
> >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[/color]
> in the webcontrol[color=green]
> >before attemping to over write it.
> >
> >anyway, thanks for your help.
> >
> >cheers
> >
> >martin.
> >
> >
> >
> >"Dune" <anonymous@discussions.microsoft.com> wrote in[/color]
> message[color=green]
> >news:046601c3d633$243d9e40$a301280a@phx.gbl...[color=darkred]
> >> i found it easier to reply under what you have already
> >> written so i could refer to specific bits...so scroll[/color][/color]
> down[color=green][color=darkred]
> >> 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[/color][/color]
> the[color=green][color=darkred]
> >> track
> >> >The following declaration is in my .aspx file
> >> >
> >> ><%@ Register TagPrefix="uc1" TagName="Header"
> >> Src="WebControls/Header.ascx"
> >> >%>
> >> ><uc1:Header id="Header1"[/color][/color]
> runat="server"></uc1:Header> -[color=green][color=darkred]
> >> -- 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[/color][/color]
> because[color=green][color=darkred]
> >> you have already put it in the html of your web form[/color][/color]
> (you[color=green][color=darkred]
> >> 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[/color][/color]
> type[color=green][color=darkred]
> >> 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[/color][/color]
> literal[color=green][color=darkred]
> >> 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[/color][/color]
> new[color=green][color=darkred]
> >> 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[/color][/color]
> the[color=green][color=darkred]
> >> 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" <anonymous@discussions.microsoft.com> wrote in
> >> message
> >> >news:036901c3d627$88df9ad0$a301280a@phx.gbl...
> >> >> When you stick your user control in your web form,[/color][/color]
> you[color=green][color=darkred]
> >> >> should give it an id (the id attribute is built-into[/color][/color]
> the[color=green][color=darkred]
> >> >> System.Web.UI.UserControl class, which your Header[/color][/color]
> user[color=green][color=darkred]
> >> >> control class is inheriting from), for example:
> >> >>
> >> >> <html>
> >> >> <body>
> >> >> ...
> >> >>
> >> >> <uc1:Header id="headerUC"[/color][/color]
> runat="server"></uc1:Header>[color=green][color=darkred]
> >> >>
> >> >> ...
> >> >> </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[/color][/color]
> name[color=green][color=darkred]
> >> >> (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-
> >> >>
> >>[/color][/color]
> us/cpref/html/frlrfsystemwebuiusercontrolmemberstopic.asp[color=green][color=darkred]
> >> >>
> >> >> 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[/color][/color]
> above[color=green][color=darkred]
> >> >> user control
> >> >> >variables from the code behind page of my web form[/color][/color]
> (not[color=green][color=darkred]
> >> >> the code behind page
> >> >> >of my user control)
> >> >> >
> >> >> >The trouble seems to be that there is no[/color][/color]
> declaration of[color=green][color=darkred]
> >> >> my user control in
> >> >> >my web form. for example the <@register ..>[/color][/color]
> directive[color=green][color=darkred]
> >> >> does not have an id
> >> >> >element that is reference in the webform.
> >> >> >
> >> >> >thank you in advance.
> >> >> >
> >> >> >any help is greatly appreciated.
> >> >> >
> >> >> >cheers
> >> >> >
> >> >> >martin.
> >> >> >
> >> >> >
> >> >> >.
> >> >> >
> >> >
> >> >
> >> >.
> >> >[/color]
> >
> >
> >.
> >[/color][/color]