accessing control's members from aspx code.

Discussion in 'ASP .Net Web Controls' started by Marko Rangel, Aug 21, 2003.

  1. Marko Rangel

    Marko Rangel Guest

    I am interested in accessing a property from a use defined web control from
    the codebehind of an aspx page.

    If my web control has a property, say Name, how would I reference the
    control in my aspx code so that I can access that instance of the control?

    I beleive (though I have not tried) to create an instance by it's full
    assembly name, but I want to use the one that is currently loaded in that
    aspx page.

    tia
    marko
     
    Marko Rangel, Aug 21, 2003
    #1
    1. Advertising

  2. Hi Marko,

    You need to get a handle on the control itself and then you can mess with its
    properties. Here's the idea, full code below:

    Dim tpban As UserControl
    Dim tpbanimage As System.Web.UI.WebControls.Image
    tpban = Page.FindControl("Tpbanner1")
    tpbanimage = tpban.FindControl("image1")
    tpbanimage.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
    tpbanimage.ID = "newid"

    Does this help?

    Ken


    <%@ Control Language="vb" AutoEventWireup="false" Codebehind="tpbanner.ascx.vb"
    Inherits="p733workev.tpbanner"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <asp:image id="Image1" runat="server"
    imageurl="http://www.gc.ca/images/englishbt.gif"></asp:image>


    Public Class tpbanner
    Inherits System.Web.UI.UserControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    End Class


    <%@ Register TagPrefix="uc1" TagName="tpbanner" Src="tpbanner.ascx" %>
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="useusercntrl.aspx.vb" Inherits="p733workev.useusercntrl"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>useusercntrl</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </head>
    <body ms_positioning="FlowLayout">
    <form id="Form1" method="post" runat="server">
    <p>
    <uc1:tpbanner id="Tpbanner1" runat="server"></uc1:tpbanner></p>
    <p>
    <asp:button id="Button1" runat="server" text="Change
    Image"></asp:button></p>
    </form>
    </body>
    </html>


    Public Class useusercntrl
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form
    Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Private Sub Button1_Click _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
    Dim tpban As UserControl
    Dim tpbanimage As System.Web.UI.WebControls.Image
    tpban = Page.FindControl("Tpbanner1")
    tpbanimage = tpban.FindControl("image1")
    tpbanimage.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
    tpbanimage.ID = "newid"
    End Sub
    End Class


    --
    Microsoft MVPs have a question for *you*: Are you patched against the Worm?
    http://www.microsoft.com/security/security_bulletins/ms03-026.asp



    "Marko Rangel" <> wrote in message
    news:...
    I am interested in accessing a property from a use defined web control from
    the codebehind of an aspx page.

    If my web control has a property, say Name, how would I reference the
    control in my aspx code so that I can access that instance of the control?

    I beleive (though I have not tried) to create an instance by it's full
    assembly name, but I want to use the one that is currently loaded in that
    aspx page.

    tia
    marko
     
    Ken Cox [Microsoft MVP], Aug 22, 2003
    #2
    1. Advertising

  3. Marko Rangel

    Marko Rangel Guest

    I think I may have reposted, so sorry if this appears twice...

    What I did, with your advice below, is have my user control registered in
    the aspx page like so:
    <%@ Register TagPrefix="ctl" TagName="controlname" Src="controlname.ascx"%>

    Instead of creating a variable with System.Web.UI.WebControls.Image (code
    below), I referenced my control. So the variable that holds my control
    should know what routines/properties are available.

    The next problem I have is that using the Page object, I could not find a
    control named "controlname" (example above). How should I be referencing
    this control if it is registered in the aspx page?

    mr


    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:...
    > Hi Marko,
    >
    > You need to get a handle on the control itself and then you can mess with

    its
    > properties. Here's the idea, full code below:
    >
    > Dim tpban As UserControl
    > Dim tpbanimage As System.Web.UI.WebControls.Image
    > tpban = Page.FindControl("Tpbanner1")
    > tpbanimage = tpban.FindControl("image1")
    > tpbanimage.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
    > tpbanimage.ID = "newid"
    >
    > Does this help?
    >
    > Ken
    >
    >
    > <%@ Control Language="vb" AutoEventWireup="false"

    Codebehind="tpbanner.ascx.vb"
    > Inherits="p733workev.tpbanner"
    > TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    > <asp:image id="Image1" runat="server"
    > imageurl="http://www.gc.ca/images/englishbt.gif"></asp:image>
    >
    >
    > Public Class tpbanner
    > Inherits System.Web.UI.UserControl
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    > Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    >
    > 'NOTE: The following placeholder declaration is required by the Web

    Form
    > Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > End Class
    >
    >
    > <%@ Register TagPrefix="uc1" TagName="tpbanner" Src="tpbanner.ascx" %>
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="useusercntrl.aspx.vb" Inherits="p733workev.useusercntrl"%>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    > <html>
    > <head>
    > <title>useusercntrl</title>
    > <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </head>
    > <body ms_positioning="FlowLayout">
    > <form id="Form1" method="post" runat="server">
    > <p>
    > <uc1:tpbanner id="Tpbanner1" runat="server"></uc1:tpbanner></p>
    > <p>
    > <asp:button id="Button1" runat="server" text="Change
    > Image"></asp:button></p>
    > </form>
    > </body>
    > </html>
    >
    >
    > Public Class useusercntrl
    > Inherits System.Web.UI.Page
    >
    > #Region " Web Form Designer Generated Code "
    >
    > 'This call is required by the Web Form Designer.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    > Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    >
    > 'NOTE: The following placeholder declaration is required by the Web

    Form
    > Designer.
    > 'Do not delete or move it.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: This method call is required by the Web Form Designer
    > 'Do not modify it using the code editor.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > End Sub
    >
    > Private Sub Button1_Click _
    > (ByVal sender As System.Object, _
    > ByVal e As System.EventArgs) _
    > Handles Button1.Click
    > Dim tpban As UserControl
    > Dim tpbanimage As System.Web.UI.WebControls.Image
    > tpban = Page.FindControl("Tpbanner1")
    > tpbanimage = tpban.FindControl("image1")
    > tpbanimage.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
    > tpbanimage.ID = "newid"
    > End Sub
    > End Class
    >
    >
    > --
    > Microsoft MVPs have a question for *you*: Are you patched against the

    Worm?
    > http://www.microsoft.com/security/security_bulletins/ms03-026.asp
    >
    >
    >
    > "Marko Rangel" <> wrote in message
    > news:...
    > I am interested in accessing a property from a use defined web control

    from
    > the codebehind of an aspx page.
    >
    > If my web control has a property, say Name, how would I reference the
    > control in my aspx code so that I can access that instance of the control?
    >
    > I beleive (though I have not tried) to create an instance by it's full
    > assembly name, but I want to use the one that is currently loaded in that
    > aspx page.
    >
    > tia
    > marko
    >
    >
    >
     
    Marko Rangel, Aug 22, 2003
    #3
    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. JFCM
    Replies:
    4
    Views:
    5,746
  2. CoolPint
    Replies:
    8
    Views:
    993
    Jeff Schwab
    Dec 14, 2003
  3. Dave
    Replies:
    3
    Views:
    370
    tom_usenet
    Aug 10, 2004
  4. hdixon
    Replies:
    3
    Views:
    650
    hdixon
    Jul 9, 2006
  5. Dave
    Replies:
    19
    Views:
    329
    David A. Coursey
    Jan 14, 2005
Loading...

Share This Page