Re: Referencing user control variables from page

Discussion in 'ASP .Net' started by martin, Jan 9, 2004.

  1. martin

    martin Guest

    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" <> wrote in message
    news:073701c3d647$4d66f140$...
    > 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" <> wrote in

    > message
    > >news:046601c3d633$243d9e40$...
    > >> 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" <> wrote in
    > >> message
    > >> >news:036901c3d627$88df9ad0$...
    > >> >> 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.
    > >> >> >
    > >> >> >
    > >> >> >.
    > >> >> >
    > >> >
    > >> >
    > >> >.
    > >> >

    > >
    > >
    > >.
    > >
     
    martin, Jan 9, 2004
    #1
    1. Advertising

  2. martin

    abhi17sep

    Joined:
    Mar 29, 2008
    Messages:
    1
    class variable in user web control?

    How can we use class variable(static) in a web user control.
    we want to store a string variable value from web control to class variable.
    but I don't access the class variable in web user control?
    please help ,thanks in advance for u
     
    abhi17sep, Mar 29, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VanReenenc
    Replies:
    1
    Views:
    492
    VanReenenc
    Jul 2, 2006
  2. =?Utf-8?B?Q2hyaXMgRGF2b2xp?=

    Referencing a User Control from ASPX page

    =?Utf-8?B?Q2hyaXMgRGF2b2xp?=, Aug 8, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    369
    =?Utf-8?B?Q2hyaXMgRGF2b2xp?=
    Aug 8, 2006
  3. BigJohn

    Referencing Repeater Data in a User Control from a Page

    BigJohn, Feb 7, 2007, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    197
    Steven Cheng[MSFT]
    Feb 8, 2007
  4. Jay Esguerra
    Replies:
    0
    Views:
    163
    Jay Esguerra
    Oct 13, 2003
  5. Chris Davoli

    Referencing User Control from ASPX page???

    Chris Davoli, Aug 8, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    159
    Chris Davoli
    Aug 8, 2006
Loading...

Share This Page