can't see properties on user control from aspx.vb

Discussion in 'ASP .Net' started by mharness, Nov 25, 2005.

  1. mharness

    mharness Guest

    Hello,

    I've tried a number of examples showing how to read the properties of a user
    control from an aspx file where the code is on the html view of the form but
    I can't figure out how to read them from the aspx.vb. Can anyone tell me
    how to declare the user control in the aspx.vb file so that I can read the
    properties?

    I did add "Protected WithEvents UC1 As System.Web.UI.UserControl" to the
    component code behind form but that's the only change I have made.

    Many thanks,

    Mike

    Here's the example from dotnetjunkies I've been working with:

    UCSub.aspc:

    <%@ Control Language="vb" AutoEventWireup="false" Src="UCSub.ascx.vb"
    Inherits="UCSub" %>
    <div align="center">
    <table style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; FONT: 10pt
    verdana; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid"
    cellspacing="10">
    <tr>
    <td>
    <asp:Label id="lblUserName" runat="server" Font-Bold="true" Text="User
    Name:" />
    </td>
    <td>
    <asp:TextBox id="txtUserName" runat="server" />
    </td>
    </tr>
    <tr>
    <td>
    <asp:Label id="lblPassword" Font-Bold="True" Text="Password:"
    runat="server" />
    </td>
    <td>
    <asp:TextBox id="txtPassword" TextMode="Password" runat="server" />
    </td>
    </tr>
    </table>
    </div>

    UCSub.ascx.vb..

    Public Class UCSub
    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 lblUserName As System.Web.UI.WebControls.Label
    Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
    Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox

    '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

    Public Sub ClearText()
    txtUserName.Text = ""
    txtPassword.Text = ""
    End Sub

    End Class

    UCSub.aspx..

    <%@ Register TagPrefix="DNJ" TagName="LoginControl" Src="UCSub.ascx" %>
    <%@ Page Language="vb" %>
    <HTML>
    <HEAD>
    <title>Using a Sub routine in a UserControl</title>
    <script runat="server">
    Sub btnSubmit_Click(Sender As Object, e As EventArgs)

    End Sub

    Sub btnClearText_Click(sender As Object, e As EventArgs)
    UC1.ClearText()
    End Sub
    </script>
    <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="form1" runat="server">
    <p></p>
    <DNJ:LoginControl id="UC1" runat="server" />
    <BR>
    <div align="center">
    <asp:Button id="btnSubmit" text="Submit" onclick="BtnSubmit_Click"
    runat="server" />
    <asp:Button id="btnClearText" text="Clear Text"
    OnClick="btnClearText_Click" runat="server" />
    <p></p>
    <asp:label id="lblMessage" runat="server" />
    </div>
    </form>
    </body>
    </HTML>

    UCSub.aspx.vb..

    Public Class UCSub1
    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 btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents btnClearText As System.Web.UI.WebControls.Button
    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents UC1 As System.Web.UI.UserControl

    '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
     
    mharness, Nov 25, 2005
    #1
    1. Advertising

  2. mharness

    mharness Guest

    Got it.

    Created a namespace around the user control class then added imports
    [solution name].[namespace] and public [user control ID] as [user
    control].ascx.vb to the aspx.vb. After declaring a public property with a
    get end get in the ascx.vb I could refer to [user control ID].[property] in
    the aspx.vb.

    I found good information and the essence of this approach in examples at
    http://www.dotnetjohn.com/articles.aspx?articleid=52 and
    http://msdn.microsoft.com/library/d...guide/html/cpconexposingpageletproperties.asp .

    Mike

    "mharness" <> wrote in message
    news:...
    > Hello,
    >
    > I've tried a number of examples showing how to read the properties of a
    > user control from an aspx file where the code is on the html view of the
    > form but I can't figure out how to read them from the aspx.vb. Can anyone
    > tell me how to declare the user control in the aspx.vb file so that I can
    > read the properties?
    >
    > I did add "Protected WithEvents UC1 As System.Web.UI.UserControl" to the
    > component code behind form but that's the only change I have made.
    >
    > Many thanks,
    >
    > Mike
    >
    > Here's the example from dotnetjunkies I've been working with:
    >
    > UCSub.aspc:
    >
    > <%@ Control Language="vb" AutoEventWireup="false" Src="UCSub.ascx.vb"
    > Inherits="UCSub" %>
    > <div align="center">
    > <table style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; FONT: 10pt
    > verdana; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid"
    > cellspacing="10">
    > <tr>
    > <td>
    > <asp:Label id="lblUserName" runat="server" Font-Bold="true" Text="User
    > Name:" />
    > </td>
    > <td>
    > <asp:TextBox id="txtUserName" runat="server" />
    > </td>
    > </tr>
    > <tr>
    > <td>
    > <asp:Label id="lblPassword" Font-Bold="True" Text="Password:"
    > runat="server" />
    > </td>
    > <td>
    > <asp:TextBox id="txtPassword" TextMode="Password" runat="server" />
    > </td>
    > </tr>
    > </table>
    > </div>
    >
    > UCSub.ascx.vb..
    >
    > Public Class UCSub
    > 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 lblUserName As System.Web.UI.WebControls.Label
    > Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
    > Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
    > Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
    >
    > '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
    >
    > Public Sub ClearText()
    > txtUserName.Text = ""
    > txtPassword.Text = ""
    > End Sub
    >
    > End Class
    >
    > UCSub.aspx..
    >
    > <%@ Register TagPrefix="DNJ" TagName="LoginControl" Src="UCSub.ascx" %>
    > <%@ Page Language="vb" %>
    > <HTML>
    > <HEAD>
    > <title>Using a Sub routine in a UserControl</title>
    > <script runat="server">
    > Sub btnSubmit_Click(Sender As Object, e As EventArgs)
    >
    > End Sub
    >
    > Sub btnClearText_Click(sender As Object, e As EventArgs)
    > UC1.ClearText()
    > End Sub
    > </script>
    > <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    > <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body>
    > <form id="form1" runat="server">
    > <p></p>
    > <DNJ:LoginControl id="UC1" runat="server" />
    > <BR>
    > <div align="center">
    > <asp:Button id="btnSubmit" text="Submit" onclick="BtnSubmit_Click"
    > runat="server" />
    > <asp:Button id="btnClearText" text="Clear Text"
    > OnClick="btnClearText_Click" runat="server" />
    > <p></p>
    > <asp:label id="lblMessage" runat="server" />
    > </div>
    > </form>
    > </body>
    > </HTML>
    >
    > UCSub.aspx.vb..
    >
    > Public Class UCSub1
    > 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 btnSubmit As System.Web.UI.WebControls.Button
    > Protected WithEvents btnClearText As System.Web.UI.WebControls.Button
    > Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
    > Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    > Protected WithEvents UC1 As System.Web.UI.UserControl
    >
    > '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
    >
    >
    >
    >
     
    mharness, Nov 28, 2005
    #2
    1. Advertising

  3. mharness

    mharness Guest

    Got it.

    Created a namespace around the user control class then added imports
    [solution name].[namespace] and public [user control ID] as [user
    control].ascx.vb to the aspx.vb. After declaring a public property with a
    get end get in the ascx.vb I could refer to [user control ID].[property] in
    the aspx.vb.

    I found good information and the essence of this approach in examples at
    http://www.dotnetjohn.com/articles.aspx?articleid=52 and
    http://msdn.microsoft.com/library/d...guide/html/cpconexposingpageletproperties.asp .

    Mike

    "mharness" <> wrote in message
    news:...
    > Hello,
    >
    > I've tried a number of examples showing how to read the properties of a
    > user control from an aspx file where the code is on the html view of the
    > form but I can't figure out how to read them from the aspx.vb. Can anyone
    > tell me how to declare the user control in the aspx.vb file so that I can
    > read the properties?
    >
    > I did add "Protected WithEvents UC1 As System.Web.UI.UserControl" to the
    > component code behind form but that's the only change I have made.
    >
    > Many thanks,
    >
    > Mike
    >
    > Here's the example from dotnetjunkies I've been working with:
    >
    > UCSub.aspc:
    >
    > <%@ Control Language="vb" AutoEventWireup="false" Src="UCSub.ascx.vb"
    > Inherits="UCSub" %>
    > <div align="center">
    > <table style="BORDER-RIGHT: 1px solid; BORDER-TOP: 1px solid; FONT: 10pt
    > verdana; BORDER-LEFT: 1px solid; BORDER-BOTTOM: 1px solid"
    > cellspacing="10">
    > <tr>
    > <td>
    > <asp:Label id="lblUserName" runat="server" Font-Bold="true" Text="User
    > Name:" />
    > </td>
    > <td>
    > <asp:TextBox id="txtUserName" runat="server" />
    > </td>
    > </tr>
    > <tr>
    > <td>
    > <asp:Label id="lblPassword" Font-Bold="True" Text="Password:"
    > runat="server" />
    > </td>
    > <td>
    > <asp:TextBox id="txtPassword" TextMode="Password" runat="server" />
    > </td>
    > </tr>
    > </table>
    > </div>
    >
    > UCSub.ascx.vb..
    >
    > Public Class UCSub
    > 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 lblUserName As System.Web.UI.WebControls.Label
    > Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
    > Protected WithEvents lblPassword As System.Web.UI.WebControls.Label
    > Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
    >
    > '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
    >
    > Public Sub ClearText()
    > txtUserName.Text = ""
    > txtPassword.Text = ""
    > End Sub
    >
    > End Class
    >
    > UCSub.aspx..
    >
    > <%@ Register TagPrefix="DNJ" TagName="LoginControl" Src="UCSub.ascx" %>
    > <%@ Page Language="vb" %>
    > <HTML>
    > <HEAD>
    > <title>Using a Sub routine in a UserControl</title>
    > <script runat="server">
    > Sub btnSubmit_Click(Sender As Object, e As EventArgs)
    >
    > End Sub
    >
    > Sub btnClearText_Click(sender As Object, e As EventArgs)
    > UC1.ClearText()
    > End Sub
    > </script>
    > <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    > <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body>
    > <form id="form1" runat="server">
    > <p></p>
    > <DNJ:LoginControl id="UC1" runat="server" />
    > <BR>
    > <div align="center">
    > <asp:Button id="btnSubmit" text="Submit" onclick="BtnSubmit_Click"
    > runat="server" />
    > <asp:Button id="btnClearText" text="Clear Text"
    > OnClick="btnClearText_Click" runat="server" />
    > <p></p>
    > <asp:label id="lblMessage" runat="server" />
    > </div>
    > </form>
    > </body>
    > </HTML>
    >
    > UCSub.aspx.vb..
    >
    > Public Class UCSub1
    > 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 btnSubmit As System.Web.UI.WebControls.Button
    > Protected WithEvents btnClearText As System.Web.UI.WebControls.Button
    > Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
    > Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    > Protected WithEvents UC1 As System.Web.UI.UserControl
    >
    > '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
    >
    >
    >
    >
     
    mharness, Dec 4, 2005
    #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. Mr Newbie
    Replies:
    3
    Views:
    8,045
  2. Flip
    Replies:
    3
    Views:
    979
    Tony Morris
    Feb 9, 2004
  3. Vahid Dadashzadeh
    Replies:
    1
    Views:
    562
    Karl Seguin [MVP]
    Jul 6, 2006
  4. Steve Kershaw
    Replies:
    1
    Views:
    364
    Brennan Stehling
    Sep 26, 2006
  5. Max2006
    Replies:
    2
    Views:
    517
    Max2006
    Jul 13, 2007
Loading...

Share This Page