example of setting a user controls properties using code behind instead of script tags?

Discussion in 'ASP .Net Web Controls' started by Dave, May 10, 2004.

  1. Dave

    Dave Guest

    hi all,

    I am looking for an example for setting/getting a user control's properties
    using a code behind page instead of <script></script> tags embedded in the
    ..aspx page.
    There seems to be millions of examples using embedded code but none actually
    showing how to do it in a code behind.

    for example: say there is a user control called "catsplash.ascx" containing
    a simple image tag:
    <asp:Image id="imgCat1" runat="server" ImageAlign="Left"></asp:Image>

    so far, I have a code behind file for the control called "catsplash.ascx.vb"

    Public Class CatSplash
    Inherits System.Web.UI.UserControl

    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()
    End Sub

    Protected WithEvents imgCat1 As System.Web.UI.WebControls.Image
    Private designerPlaceholderDeclaration As System.Object
    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    InitializeComponent()
    End Sub

    Private sImageURL1 As String

    Public Property imageurl1() As String
    Get
    Return sImageURL1
    End Get
    Set(ByVal Value As String)
    sImageURL1 = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load

    imgCat1.ImageUrl = sImageURL1

    End Sub
    End Class


    Then, in the .aspx page containing the control (category.aspx), I have
    <%@ Page CodeBehind="category.aspx.vb" Language="vb" AutoEventWireup="false"
    Inherits="BVCommerce.category" %>
    <%@ Register TagPrefix="uc1" TagName="CatSplash"
    Src="controls/CatSplash.ascx" %>
    ....
    <uc1:catsplash id="CatSplash1" runat="server"
    Visible="False"></uc1:catsplash>
    ....


    how do I reference the control in the codebehind? (category.aspx.vb)
    for example, I just want to set the image url to something else.
    but the following does not work in the code behind:

    CatSplash.imageurl1 = "images/mycategoryheader.jpg"

    I am not sure how I need to reference this - "CatSplash" is the name of the
    class as you can see above and I have a public property called "imageurl1",
    yet it does not work as expected. (in the books, they reference it by the
    id of the control inside a <script> tag, but this won't work either in a
    code behind)

    What am I doing wrong here?

    TIA,
    Dave
     
    Dave, May 10, 2004
    #1
    1. Advertising

  2. Dave

    Andy Gaskell Guest

    Try removing the code form Page_Load and change your property to this. You
    can also remove the private field (sImageURL1).

    Public Property imageurl1() As String
    Get
    Return Me.imgCat1.ImageUrl
    End Get
    Set(ByVal Value As String)
    Me.imgCat1.ImageUrl = Value
    End Set
    End Property

    "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    news:%...
    > hi all,
    >
    > I am looking for an example for setting/getting a user control's

    properties
    > using a code behind page instead of <script></script> tags embedded in the
    > .aspx page.
    > There seems to be millions of examples using embedded code but none

    actually
    > showing how to do it in a code behind.
    >
    > for example: say there is a user control called "catsplash.ascx"

    containing
    > a simple image tag:
    > <asp:Image id="imgCat1" runat="server" ImageAlign="Left"></asp:Image>
    >
    > so far, I have a code behind file for the control called

    "catsplash.ascx.vb"
    >
    > Public Class CatSplash
    > Inherits System.Web.UI.UserControl
    >
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    > End Sub
    >
    > Protected WithEvents imgCat1 As System.Web.UI.WebControls.Image
    > Private designerPlaceholderDeclaration As System.Object
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > InitializeComponent()
    > End Sub
    >
    > Private sImageURL1 As String
    >
    > Public Property imageurl1() As String
    > Get
    > Return sImageURL1
    > End Get
    > Set(ByVal Value As String)
    > sImageURL1 = Value
    > End Set
    > End Property
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    >
    > imgCat1.ImageUrl = sImageURL1
    >
    > End Sub
    > End Class
    >
    >
    > Then, in the .aspx page containing the control (category.aspx), I have
    > <%@ Page CodeBehind="category.aspx.vb" Language="vb"

    AutoEventWireup="false"
    > Inherits="BVCommerce.category" %>
    > <%@ Register TagPrefix="uc1" TagName="CatSplash"
    > Src="controls/CatSplash.ascx" %>
    > ...
    > <uc1:catsplash id="CatSplash1" runat="server"
    > Visible="False"></uc1:catsplash>
    > ...
    >
    >
    > how do I reference the control in the codebehind? (category.aspx.vb)
    > for example, I just want to set the image url to something else.
    > but the following does not work in the code behind:
    >
    > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    >
    > I am not sure how I need to reference this - "CatSplash" is the name of

    the
    > class as you can see above and I have a public property called

    "imageurl1",
    > yet it does not work as expected. (in the books, they reference it by the
    > id of the control inside a <script> tag, but this won't work either in a
    > code behind)
    >
    > What am I doing wrong here?
    >
    > TIA,
    > Dave
    >
    >
     
    Andy Gaskell, May 10, 2004
    #2
    1. Advertising

  3. Dave

    Dave Guest

    AG,
    I tried that with the same results: "Object reference not set to an instance
    of an object."
    after running the result.
    I changing the code in the category.aspx.vb to

    Dim cs as CatSplash
    cs.imageurl1 = "images/mycategoryheader.jpg"

    allowed me to actually compile the page,
    otherwise I get "Reference to a non-shared member requires an object
    reference."
    when using:
    CatSplash.imageurl1 = "images/mycategoryheader.jpg"

    I tried also to change public property to read
    Public Shared Property imageurl1 as string ....

    but it mentions needing an instance of a class ...yada yada yada...
    I think the problem is that this is a circular reference (like it has to be
    defined before it is defined)

    I'm stumped.




    "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    news:ur%...
    > Try removing the code form Page_Load and change your property to this. You
    > can also remove the private field (sImageURL1).
    >
    > Public Property imageurl1() As String
    > Get
    > Return Me.imgCat1.ImageUrl
    > End Get
    > Set(ByVal Value As String)
    > Me.imgCat1.ImageUrl = Value
    > End Set
    > End Property
    >
    > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    > news:%...
    > > hi all,
    > >
    > > I am looking for an example for setting/getting a user control's

    > properties
    > > using a code behind page instead of <script></script> tags embedded in

    the
    > > .aspx page.
    > > There seems to be millions of examples using embedded code but none

    > actually
    > > showing how to do it in a code behind.
    > >
    > > for example: say there is a user control called "catsplash.ascx"

    > containing
    > > a simple image tag:
    > > <asp:Image id="imgCat1" runat="server" ImageAlign="Left"></asp:Image>
    > >
    > > so far, I have a code behind file for the control called

    > "catsplash.ascx.vb"
    > >
    > > Public Class CatSplash
    > > Inherits System.Web.UI.UserControl
    > >
    > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > InitializeComponent()
    > > End Sub
    > >
    > > Protected WithEvents imgCat1 As System.Web.UI.WebControls.Image
    > > Private designerPlaceholderDeclaration As System.Object
    > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Init
    > > InitializeComponent()
    > > End Sub
    > >
    > > Private sImageURL1 As String
    > >
    > > Public Property imageurl1() As String
    > > Get
    > > Return sImageURL1
    > > End Get
    > > Set(ByVal Value As String)
    > > sImageURL1 = Value
    > > End Set
    > > End Property
    > >
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > >
    > > imgCat1.ImageUrl = sImageURL1
    > >
    > > End Sub
    > > End Class
    > >
    > >
    > > Then, in the .aspx page containing the control (category.aspx), I have
    > > <%@ Page CodeBehind="category.aspx.vb" Language="vb"

    > AutoEventWireup="false"
    > > Inherits="BVCommerce.category" %>
    > > <%@ Register TagPrefix="uc1" TagName="CatSplash"
    > > Src="controls/CatSplash.ascx" %>
    > > ...
    > > <uc1:catsplash id="CatSplash1" runat="server"
    > > Visible="False"></uc1:catsplash>
    > > ...
    > >
    > >
    > > how do I reference the control in the codebehind? (category.aspx.vb)
    > > for example, I just want to set the image url to something else.
    > > but the following does not work in the code behind:
    > >
    > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > >
    > > I am not sure how I need to reference this - "CatSplash" is the name of

    > the
    > > class as you can see above and I have a public property called

    > "imageurl1",
    > > yet it does not work as expected. (in the books, they reference it by

    the
    > > id of the control inside a <script> tag, but this won't work either in

    a
    > > code behind)
    > >
    > > What am I doing wrong here?
    > >
    > > TIA,
    > > Dave
    > >
    > >

    >
    >
     
    Dave, May 10, 2004
    #3
  4. Dave

    Andy Gaskell Guest

    You don't want to change the property to shared. I noticed you already have
    the tag in the HTML so the codebehind of the page (most likely) already has
    declared that control.

    try this:

    CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"

    instead of:

    Dim cs as CatSplash
    cs.imageurl1 = "images/mycategoryheader.jpg"


    "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    news:...
    > AG,
    > I tried that with the same results: "Object reference not set to an

    instance
    > of an object."
    > after running the result.
    > I changing the code in the category.aspx.vb to
    >
    > Dim cs as CatSplash
    > cs.imageurl1 = "images/mycategoryheader.jpg"
    >
    > allowed me to actually compile the page,
    > otherwise I get "Reference to a non-shared member requires an object
    > reference."
    > when using:
    > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    >
    > I tried also to change public property to read
    > Public Shared Property imageurl1 as string ....
    >
    > but it mentions needing an instance of a class ...yada yada yada...
    > I think the problem is that this is a circular reference (like it has to

    be
    > defined before it is defined)
    >
    > I'm stumped.
    >
    >
    >
    >
    > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > news:ur%...
    > > Try removing the code form Page_Load and change your property to this.

    You
    > > can also remove the private field (sImageURL1).
    > >
    > > Public Property imageurl1() As String
    > > Get
    > > Return Me.imgCat1.ImageUrl
    > > End Get
    > > Set(ByVal Value As String)
    > > Me.imgCat1.ImageUrl = Value
    > > End Set
    > > End Property
    > >
    > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    > > news:%...
    > > > hi all,
    > > >
    > > > I am looking for an example for setting/getting a user control's

    > > properties
    > > > using a code behind page instead of <script></script> tags embedded in

    > the
    > > > .aspx page.
    > > > There seems to be millions of examples using embedded code but none

    > > actually
    > > > showing how to do it in a code behind.
    > > >
    > > > for example: say there is a user control called "catsplash.ascx"

    > > containing
    > > > a simple image tag:
    > > > <asp:Image id="imgCat1" runat="server" ImageAlign="Left"></asp:Image>
    > > >
    > > > so far, I have a code behind file for the control called

    > > "catsplash.ascx.vb"
    > > >
    > > > Public Class CatSplash
    > > > Inherits System.Web.UI.UserControl
    > > >
    > > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > > InitializeComponent()
    > > > End Sub
    > > >
    > > > Protected WithEvents imgCat1 As System.Web.UI.WebControls.Image
    > > > Private designerPlaceholderDeclaration As System.Object
    > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs) Handles MyBase.Init
    > > > InitializeComponent()
    > > > End Sub
    > > >
    > > > Private sImageURL1 As String
    > > >
    > > > Public Property imageurl1() As String
    > > > Get
    > > > Return sImageURL1
    > > > End Get
    > > > Set(ByVal Value As String)
    > > > sImageURL1 = Value
    > > > End Set
    > > > End Property
    > > >
    > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs) Handles MyBase.Load
    > > >
    > > > imgCat1.ImageUrl = sImageURL1
    > > >
    > > > End Sub
    > > > End Class
    > > >
    > > >
    > > > Then, in the .aspx page containing the control (category.aspx), I have
    > > > <%@ Page CodeBehind="category.aspx.vb" Language="vb"

    > > AutoEventWireup="false"
    > > > Inherits="BVCommerce.category" %>
    > > > <%@ Register TagPrefix="uc1" TagName="CatSplash"
    > > > Src="controls/CatSplash.ascx" %>
    > > > ...
    > > > <uc1:catsplash id="CatSplash1" runat="server"
    > > > Visible="False"></uc1:catsplash>
    > > > ...
    > > >
    > > >
    > > > how do I reference the control in the codebehind? (category.aspx.vb)
    > > > for example, I just want to set the image url to something else.
    > > > but the following does not work in the code behind:
    > > >
    > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > >
    > > > I am not sure how I need to reference this - "CatSplash" is the name

    of
    > > the
    > > > class as you can see above and I have a public property called

    > > "imageurl1",
    > > > yet it does not work as expected. (in the books, they reference it by

    > the
    > > > id of the control inside a <script> tag, but this won't work either

    in
    > a
    > > > code behind)
    > > >
    > > > What am I doing wrong here?
    > > >
    > > > TIA,
    > > > Dave
    > > >
    > > >

    > >
    > >

    >
    >
     
    Andy Gaskell, May 10, 2004
    #4
  5. Dave

    Dave Guest

    I looked in the code behind and there is no declaration of the user control.
    That would explain why CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"
    does not work.
    Do you know how to declare it?


    "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    news:...
    > You don't want to change the property to shared. I noticed you already

    have
    > the tag in the HTML so the codebehind of the page (most likely) already

    has
    > declared that control.
    >
    > try this:
    >
    > CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"
    >
    > instead of:
    >
    > Dim cs as CatSplash
    > cs.imageurl1 = "images/mycategoryheader.jpg"
    >
    >
    > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    > news:...
    > > AG,
    > > I tried that with the same results: "Object reference not set to an

    > instance
    > > of an object."
    > > after running the result.
    > > I changing the code in the category.aspx.vb to
    > >
    > > Dim cs as CatSplash
    > > cs.imageurl1 = "images/mycategoryheader.jpg"
    > >
    > > allowed me to actually compile the page,
    > > otherwise I get "Reference to a non-shared member requires an object
    > > reference."
    > > when using:
    > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > >
    > > I tried also to change public property to read
    > > Public Shared Property imageurl1 as string ....
    > >
    > > but it mentions needing an instance of a class ...yada yada yada...
    > > I think the problem is that this is a circular reference (like it has to

    > be
    > > defined before it is defined)
    > >
    > > I'm stumped.
    > >
    > >
    > >
    > >
    > > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > > news:ur%...
    > > > Try removing the code form Page_Load and change your property to this.

    > You
    > > > can also remove the private field (sImageURL1).
    > > >
    > > > Public Property imageurl1() As String
    > > > Get
    > > > Return Me.imgCat1.ImageUrl
    > > > End Get
    > > > Set(ByVal Value As String)
    > > > Me.imgCat1.ImageUrl = Value
    > > > End Set
    > > > End Property
    > > >
    > > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in

    message
    > > > news:%...
    > > > > hi all,
    > > > >
    > > > > I am looking for an example for setting/getting a user control's
    > > > properties
    > > > > using a code behind page instead of <script></script> tags embedded

    in
    > > the
    > > > > .aspx page.
    > > > > There seems to be millions of examples using embedded code but none
    > > > actually
    > > > > showing how to do it in a code behind.
    > > > >
    > > > > for example: say there is a user control called "catsplash.ascx"
    > > > containing
    > > > > a simple image tag:
    > > > > <asp:Image id="imgCat1" runat="server"

    ImageAlign="Left"></asp:Image>
    > > > >
    > > > > so far, I have a code behind file for the control called
    > > > "catsplash.ascx.vb"
    > > > >
    > > > > Public Class CatSplash
    > > > > Inherits System.Web.UI.UserControl
    > > > >
    > > > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > > > InitializeComponent()
    > > > > End Sub
    > > > >
    > > > > Protected WithEvents imgCat1 As System.Web.UI.WebControls.Image
    > > > > Private designerPlaceholderDeclaration As System.Object
    > > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > > > > System.EventArgs) Handles MyBase.Init
    > > > > InitializeComponent()
    > > > > End Sub
    > > > >
    > > > > Private sImageURL1 As String
    > > > >
    > > > > Public Property imageurl1() As String
    > > > > Get
    > > > > Return sImageURL1
    > > > > End Get
    > > > > Set(ByVal Value As String)
    > > > > sImageURL1 = Value
    > > > > End Set
    > > > > End Property
    > > > >
    > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > > System.EventArgs) Handles MyBase.Load
    > > > >
    > > > > imgCat1.ImageUrl = sImageURL1
    > > > >
    > > > > End Sub
    > > > > End Class
    > > > >
    > > > >
    > > > > Then, in the .aspx page containing the control (category.aspx), I

    have
    > > > > <%@ Page CodeBehind="category.aspx.vb" Language="vb"
    > > > AutoEventWireup="false"
    > > > > Inherits="BVCommerce.category" %>
    > > > > <%@ Register TagPrefix="uc1" TagName="CatSplash"
    > > > > Src="controls/CatSplash.ascx" %>
    > > > > ...
    > > > > <uc1:catsplash id="CatSplash1" runat="server"
    > > > > Visible="False"></uc1:catsplash>
    > > > > ...
    > > > >
    > > > >
    > > > > how do I reference the control in the codebehind? (category.aspx.vb)
    > > > > for example, I just want to set the image url to something else.
    > > > > but the following does not work in the code behind:
    > > > >
    > > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > > >
    > > > > I am not sure how I need to reference this - "CatSplash" is the name

    > of
    > > > the
    > > > > class as you can see above and I have a public property called
    > > > "imageurl1",
    > > > > yet it does not work as expected. (in the books, they reference it

    by
    > > the
    > > > > id of the control inside a <script> tag, but this won't work either

    > in
    > > a
    > > > > code behind)
    > > > >
    > > > > What am I doing wrong here?
    > > > >
    > > > > TIA,
    > > > > Dave
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Dave, May 10, 2004
    #5
  6. Dave

    Andy Gaskell Guest

    Put this right under the Inherits line

    Protected WithEvents CatSplash1 As CatSplash


    "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    news:...
    > I looked in the code behind and there is no declaration of the user

    control.
    > That would explain why CatSplash1.ImageUrl1 =

    "images/mycategoryheader.jpg"
    > does not work.
    > Do you know how to declare it?
    >
    >
    > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > news:...
    > > You don't want to change the property to shared. I noticed you already

    > have
    > > the tag in the HTML so the codebehind of the page (most likely) already

    > has
    > > declared that control.
    > >
    > > try this:
    > >
    > > CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"
    > >
    > > instead of:
    > >
    > > Dim cs as CatSplash
    > > cs.imageurl1 = "images/mycategoryheader.jpg"
    > >
    > >
    > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    > > news:...
    > > > AG,
    > > > I tried that with the same results: "Object reference not set to an

    > > instance
    > > > of an object."
    > > > after running the result.
    > > > I changing the code in the category.aspx.vb to
    > > >
    > > > Dim cs as CatSplash
    > > > cs.imageurl1 = "images/mycategoryheader.jpg"
    > > >
    > > > allowed me to actually compile the page,
    > > > otherwise I get "Reference to a non-shared member requires an object
    > > > reference."
    > > > when using:
    > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > >
    > > > I tried also to change public property to read
    > > > Public Shared Property imageurl1 as string ....
    > > >
    > > > but it mentions needing an instance of a class ...yada yada yada...
    > > > I think the problem is that this is a circular reference (like it has

    to
    > > be
    > > > defined before it is defined)
    > > >
    > > > I'm stumped.
    > > >
    > > >
    > > >
    > > >
    > > > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > > > news:ur%...
    > > > > Try removing the code form Page_Load and change your property to

    this.
    > > You
    > > > > can also remove the private field (sImageURL1).
    > > > >
    > > > > Public Property imageurl1() As String
    > > > > Get
    > > > > Return Me.imgCat1.ImageUrl
    > > > > End Get
    > > > > Set(ByVal Value As String)
    > > > > Me.imgCat1.ImageUrl = Value
    > > > > End Set
    > > > > End Property
    > > > >
    > > > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in

    > message
    > > > > news:%...
    > > > > > hi all,
    > > > > >
    > > > > > I am looking for an example for setting/getting a user control's
    > > > > properties
    > > > > > using a code behind page instead of <script></script> tags

    embedded
    > in
    > > > the
    > > > > > .aspx page.
    > > > > > There seems to be millions of examples using embedded code but

    none
    > > > > actually
    > > > > > showing how to do it in a code behind.
    > > > > >
    > > > > > for example: say there is a user control called "catsplash.ascx"
    > > > > containing
    > > > > > a simple image tag:
    > > > > > <asp:Image id="imgCat1" runat="server"

    > ImageAlign="Left"></asp:Image>
    > > > > >
    > > > > > so far, I have a code behind file for the control called
    > > > > "catsplash.ascx.vb"
    > > > > >
    > > > > > Public Class CatSplash
    > > > > > Inherits System.Web.UI.UserControl
    > > > > >
    > > > > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > > > > InitializeComponent()
    > > > > > End Sub
    > > > > >
    > > > > > Protected WithEvents imgCat1 As

    System.Web.UI.WebControls.Image
    > > > > > Private designerPlaceholderDeclaration As System.Object
    > > > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e

    As
    > > > > > System.EventArgs) Handles MyBase.Init
    > > > > > InitializeComponent()
    > > > > > End Sub
    > > > > >
    > > > > > Private sImageURL1 As String
    > > > > >
    > > > > > Public Property imageurl1() As String
    > > > > > Get
    > > > > > Return sImageURL1
    > > > > > End Get
    > > > > > Set(ByVal Value As String)
    > > > > > sImageURL1 = Value
    > > > > > End Set
    > > > > > End Property
    > > > > >
    > > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e

    As
    > > > > > System.EventArgs) Handles MyBase.Load
    > > > > >
    > > > > > imgCat1.ImageUrl = sImageURL1
    > > > > >
    > > > > > End Sub
    > > > > > End Class
    > > > > >
    > > > > >
    > > > > > Then, in the .aspx page containing the control (category.aspx), I

    > have
    > > > > > <%@ Page CodeBehind="category.aspx.vb" Language="vb"
    > > > > AutoEventWireup="false"
    > > > > > Inherits="BVCommerce.category" %>
    > > > > > <%@ Register TagPrefix="uc1" TagName="CatSplash"
    > > > > > Src="controls/CatSplash.ascx" %>
    > > > > > ...
    > > > > > <uc1:catsplash id="CatSplash1" runat="server"
    > > > > > Visible="False"></uc1:catsplash>
    > > > > > ...
    > > > > >
    > > > > >
    > > > > > how do I reference the control in the codebehind?

    (category.aspx.vb)
    > > > > > for example, I just want to set the image url to something else.
    > > > > > but the following does not work in the code behind:
    > > > > >
    > > > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > > > >
    > > > > > I am not sure how I need to reference this - "CatSplash" is the

    name
    > > of
    > > > > the
    > > > > > class as you can see above and I have a public property called
    > > > > "imageurl1",
    > > > > > yet it does not work as expected. (in the books, they reference

    it
    > by
    > > > the
    > > > > > id of the control inside a <script> tag, but this won't work

    either
    > > in
    > > > a
    > > > > > code behind)
    > > > > >
    > > > > > What am I doing wrong here?
    > > > > >
    > > > > > TIA,
    > > > > > Dave
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Andy Gaskell, May 10, 2004
    #6
  7. Dave

    Dave Guest

    DOH! That was it! (note to self: declare the control in the main class,
    not inside a sub like "page_load")
    One day a neuron in my brain will fire and OOP will sink in - that day has
    yet to come. ;)
    Thanks AG!

    "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    news:Ofi%...
    > Put this right under the Inherits line
    >
    > Protected WithEvents CatSplash1 As CatSplash
    >
    >
    > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    > news:...
    > > I looked in the code behind and there is no declaration of the user

    > control.
    > > That would explain why CatSplash1.ImageUrl1 =

    > "images/mycategoryheader.jpg"
    > > does not work.
    > > Do you know how to declare it?
    > >
    > >
    > > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > > news:...
    > > > You don't want to change the property to shared. I noticed you already

    > > have
    > > > the tag in the HTML so the codebehind of the page (most likely)

    already
    > > has
    > > > declared that control.
    > > >
    > > > try this:
    > > >
    > > > CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"
    > > >
    > > > instead of:
    > > >
    > > > Dim cs as CatSplash
    > > > cs.imageurl1 = "images/mycategoryheader.jpg"
    > > >
    > > >
    > > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in

    message
    > > > news:...
    > > > > AG,
    > > > > I tried that with the same results: "Object reference not set to an
    > > > instance
    > > > > of an object."
    > > > > after running the result.
    > > > > I changing the code in the category.aspx.vb to
    > > > >
    > > > > Dim cs as CatSplash
    > > > > cs.imageurl1 = "images/mycategoryheader.jpg"
    > > > >
    > > > > allowed me to actually compile the page,
    > > > > otherwise I get "Reference to a non-shared member requires an object
    > > > > reference."
    > > > > when using:
    > > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > > >
    > > > > I tried also to change public property to read
    > > > > Public Shared Property imageurl1 as string ....
    > > > >
    > > > > but it mentions needing an instance of a class ...yada yada yada...
    > > > > I think the problem is that this is a circular reference (like it

    has
    > to
    > > > be
    > > > > defined before it is defined)
    > > > >
    > > > > I'm stumped.
    > > > >
    > > > >
    > > > >
    > > > >
    > > > > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > > > > news:ur%...
    > > > > > Try removing the code form Page_Load and change your property to

    > this.
    > > > You
    > > > > > can also remove the private field (sImageURL1).
    > > > > >
    > > > > > Public Property imageurl1() As String
    > > > > > Get
    > > > > > Return Me.imgCat1.ImageUrl
    > > > > > End Get
    > > > > > Set(ByVal Value As String)
    > > > > > Me.imgCat1.ImageUrl = Value
    > > > > > End Set
    > > > > > End Property
    > > > > >
    > > > > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in

    > > message
    > > > > > news:%...
    > > > > > > hi all,
    > > > > > >
    > > > > > > I am looking for an example for setting/getting a user control's
    > > > > > properties
    > > > > > > using a code behind page instead of <script></script> tags

    > embedded
    > > in
    > > > > the
    > > > > > > .aspx page.
    > > > > > > There seems to be millions of examples using embedded code but

    > none
    > > > > > actually
    > > > > > > showing how to do it in a code behind.
    > > > > > >
    > > > > > > for example: say there is a user control called "catsplash.ascx"
    > > > > > containing
    > > > > > > a simple image tag:
    > > > > > > <asp:Image id="imgCat1" runat="server"

    > > ImageAlign="Left"></asp:Image>
    > > > > > >
    > > > > > > so far, I have a code behind file for the control called
    > > > > > "catsplash.ascx.vb"
    > > > > > >
    > > > > > > Public Class CatSplash
    > > > > > > Inherits System.Web.UI.UserControl
    > > > > > >
    > > > > > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > > > > > InitializeComponent()
    > > > > > > End Sub
    > > > > > >
    > > > > > > Protected WithEvents imgCat1 As

    > System.Web.UI.WebControls.Image
    > > > > > > Private designerPlaceholderDeclaration As System.Object
    > > > > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e

    > As
    > > > > > > System.EventArgs) Handles MyBase.Init
    > > > > > > InitializeComponent()
    > > > > > > End Sub
    > > > > > >
    > > > > > > Private sImageURL1 As String
    > > > > > >
    > > > > > > Public Property imageurl1() As String
    > > > > > > Get
    > > > > > > Return sImageURL1
    > > > > > > End Get
    > > > > > > Set(ByVal Value As String)
    > > > > > > sImageURL1 = Value
    > > > > > > End Set
    > > > > > > End Property
    > > > > > >
    > > > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e

    > As
    > > > > > > System.EventArgs) Handles MyBase.Load
    > > > > > >
    > > > > > > imgCat1.ImageUrl = sImageURL1
    > > > > > >
    > > > > > > End Sub
    > > > > > > End Class
    > > > > > >
    > > > > > >
    > > > > > > Then, in the .aspx page containing the control (category.aspx),

    I
    > > have
    > > > > > > <%@ Page CodeBehind="category.aspx.vb" Language="vb"
    > > > > > AutoEventWireup="false"
    > > > > > > Inherits="BVCommerce.category" %>
    > > > > > > <%@ Register TagPrefix="uc1" TagName="CatSplash"
    > > > > > > Src="controls/CatSplash.ascx" %>
    > > > > > > ...
    > > > > > > <uc1:catsplash id="CatSplash1" runat="server"
    > > > > > > Visible="False"></uc1:catsplash>
    > > > > > > ...
    > > > > > >
    > > > > > >
    > > > > > > how do I reference the control in the codebehind?

    > (category.aspx.vb)
    > > > > > > for example, I just want to set the image url to something else.
    > > > > > > but the following does not work in the code behind:
    > > > > > >
    > > > > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > > > > >
    > > > > > > I am not sure how I need to reference this - "CatSplash" is the

    > name
    > > > of
    > > > > > the
    > > > > > > class as you can see above and I have a public property called
    > > > > > "imageurl1",
    > > > > > > yet it does not work as expected. (in the books, they reference

    > it
    > > by
    > > > > the
    > > > > > > id of the control inside a <script> tag, but this won't work

    > either
    > > > in
    > > > > a
    > > > > > > code behind)
    > > > > > >
    > > > > > > What am I doing wrong here?
    > > > > > >
    > > > > > > TIA,
    > > > > > > Dave
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Dave, May 10, 2004
    #7
  8. Dave

    Andy Gaskell Guest

    No problem - good luck!

    "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    news:...
    > DOH! That was it! (note to self: declare the control in the main class,
    > not inside a sub like "page_load")
    > One day a neuron in my brain will fire and OOP will sink in - that day has
    > yet to come. ;)
    > Thanks AG!
    >
    > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > news:Ofi%...
    > > Put this right under the Inherits line
    > >
    > > Protected WithEvents CatSplash1 As CatSplash
    > >
    > >
    > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in message
    > > news:...
    > > > I looked in the code behind and there is no declaration of the user

    > > control.
    > > > That would explain why CatSplash1.ImageUrl1 =

    > > "images/mycategoryheader.jpg"
    > > > does not work.
    > > > Do you know how to declare it?
    > > >
    > > >
    > > > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > > > news:...
    > > > > You don't want to change the property to shared. I noticed you

    already
    > > > have
    > > > > the tag in the HTML so the codebehind of the page (most likely)

    > already
    > > > has
    > > > > declared that control.
    > > > >
    > > > > try this:
    > > > >
    > > > > CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"
    > > > >
    > > > > instead of:
    > > > >
    > > > > Dim cs as CatSplash
    > > > > cs.imageurl1 = "images/mycategoryheader.jpg"
    > > > >
    > > > >
    > > > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in

    > message
    > > > > news:...
    > > > > > AG,
    > > > > > I tried that with the same results: "Object reference not set to

    an
    > > > > instance
    > > > > > of an object."
    > > > > > after running the result.
    > > > > > I changing the code in the category.aspx.vb to
    > > > > >
    > > > > > Dim cs as CatSplash
    > > > > > cs.imageurl1 = "images/mycategoryheader.jpg"
    > > > > >
    > > > > > allowed me to actually compile the page,
    > > > > > otherwise I get "Reference to a non-shared member requires an

    object
    > > > > > reference."
    > > > > > when using:
    > > > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > > > >
    > > > > > I tried also to change public property to read
    > > > > > Public Shared Property imageurl1 as string ....
    > > > > >
    > > > > > but it mentions needing an instance of a class ...yada yada

    yada...
    > > > > > I think the problem is that this is a circular reference (like it

    > has
    > > to
    > > > > be
    > > > > > defined before it is defined)
    > > > > >
    > > > > > I'm stumped.
    > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > > > > "Andy Gaskell" <pubb AT hotmail DOT com> wrote in message
    > > > > > news:ur%...
    > > > > > > Try removing the code form Page_Load and change your property to

    > > this.
    > > > > You
    > > > > > > can also remove the private field (sImageURL1).
    > > > > > >
    > > > > > > Public Property imageurl1() As String
    > > > > > > Get
    > > > > > > Return Me.imgCat1.ImageUrl
    > > > > > > End Get
    > > > > > > Set(ByVal Value As String)
    > > > > > > Me.imgCat1.ImageUrl = Value
    > > > > > > End Set
    > > > > > > End Property
    > > > > > >
    > > > > > > "Dave" <dave(delete_this)@miraclecatDELETETHISTOO.com> wrote in
    > > > message
    > > > > > > news:%...
    > > > > > > > hi all,
    > > > > > > >
    > > > > > > > I am looking for an example for setting/getting a user

    control's
    > > > > > > properties
    > > > > > > > using a code behind page instead of <script></script> tags

    > > embedded
    > > > in
    > > > > > the
    > > > > > > > .aspx page.
    > > > > > > > There seems to be millions of examples using embedded code but

    > > none
    > > > > > > actually
    > > > > > > > showing how to do it in a code behind.
    > > > > > > >
    > > > > > > > for example: say there is a user control called

    "catsplash.ascx"
    > > > > > > containing
    > > > > > > > a simple image tag:
    > > > > > > > <asp:Image id="imgCat1" runat="server"
    > > > ImageAlign="Left"></asp:Image>
    > > > > > > >
    > > > > > > > so far, I have a code behind file for the control called
    > > > > > > "catsplash.ascx.vb"
    > > > > > > >
    > > > > > > > Public Class CatSplash
    > > > > > > > Inherits System.Web.UI.UserControl
    > > > > > > >
    > > > > > > > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > > > > > > > InitializeComponent()
    > > > > > > > End Sub
    > > > > > > >
    > > > > > > > Protected WithEvents imgCat1 As

    > > System.Web.UI.WebControls.Image
    > > > > > > > Private designerPlaceholderDeclaration As System.Object
    > > > > > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal

    e
    > > As
    > > > > > > > System.EventArgs) Handles MyBase.Init
    > > > > > > > InitializeComponent()
    > > > > > > > End Sub
    > > > > > > >
    > > > > > > > Private sImageURL1 As String
    > > > > > > >
    > > > > > > > Public Property imageurl1() As String
    > > > > > > > Get
    > > > > > > > Return sImageURL1
    > > > > > > > End Get
    > > > > > > > Set(ByVal Value As String)
    > > > > > > > sImageURL1 = Value
    > > > > > > > End Set
    > > > > > > > End Property
    > > > > > > >
    > > > > > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal

    e
    > > As
    > > > > > > > System.EventArgs) Handles MyBase.Load
    > > > > > > >
    > > > > > > > imgCat1.ImageUrl = sImageURL1
    > > > > > > >
    > > > > > > > End Sub
    > > > > > > > End Class
    > > > > > > >
    > > > > > > >
    > > > > > > > Then, in the .aspx page containing the control

    (category.aspx),
    > I
    > > > have
    > > > > > > > <%@ Page CodeBehind="category.aspx.vb" Language="vb"
    > > > > > > AutoEventWireup="false"
    > > > > > > > Inherits="BVCommerce.category" %>
    > > > > > > > <%@ Register TagPrefix="uc1" TagName="CatSplash"
    > > > > > > > Src="controls/CatSplash.ascx" %>
    > > > > > > > ...
    > > > > > > > <uc1:catsplash id="CatSplash1" runat="server"
    > > > > > > > Visible="False"></uc1:catsplash>
    > > > > > > > ...
    > > > > > > >
    > > > > > > >
    > > > > > > > how do I reference the control in the codebehind?

    > > (category.aspx.vb)
    > > > > > > > for example, I just want to set the image url to something

    else.
    > > > > > > > but the following does not work in the code behind:
    > > > > > > >
    > > > > > > > CatSplash.imageurl1 = "images/mycategoryheader.jpg"
    > > > > > > >
    > > > > > > > I am not sure how I need to reference this - "CatSplash" is

    the
    > > name
    > > > > of
    > > > > > > the
    > > > > > > > class as you can see above and I have a public property called
    > > > > > > "imageurl1",
    > > > > > > > yet it does not work as expected. (in the books, they

    reference
    > > it
    > > > by
    > > > > > the
    > > > > > > > id of the control inside a <script> tag, but this won't work

    > > either
    > > > > in
    > > > > > a
    > > > > > > > code behind)
    > > > > > > >
    > > > > > > > What am I doing wrong here?
    > > > > > > >
    > > > > > > > TIA,
    > > > > > > > Dave
    > > > > > > >
    > > > > > > >
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Andy Gaskell, May 11, 2004
    #8
    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. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    594
    Alphonse Giambrone
    Jun 28, 2003
  2. aaa
    Replies:
    3
    Views:
    4,789
    Verde
    May 6, 2005
  3. Replies:
    0
    Views:
    479
  4. Replies:
    2
    Views:
    1,108
  5. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    491
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
Loading...

Share This Page