How do I set the property for a Web User Control properties from the parent form (VB)

Discussion in 'ASP .Net Web Controls' started by David Webb, Dec 30, 2004.

  1. David Webb

    David Webb Guest

    I am having trouble setting the property to a Web User Control from the
    Parent page.

    Here's the Web User Control: WebUserControl1.ascx
    HTML:-----------------------------------------------------------
    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="WebUserControl1.ascx.vb" Inherits="TEST.WebUserControl1"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <DIV align="center">
    <asp:Label id="Label1" runat="server">Label</asp:Label></DIV>
    -----------------------------------------------------------


    CodeBehind:-----------------------------------------------------------
    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.HtmlControls


    Public Class WebUserControl1
    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

    '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

    Protected WithEvents Label1 As System.Web.UI.WebControls.Label

    Public Property LabelText() As String
    Get
    Return Label1.Text
    End Get
    Set(ByVal Value As String)
    Label1.Text = Value
    End Set
    End Property

    End Class
    -----------------------------------------------------------

    Here's the Parent form: ParentForm.aspx
    HTML:-----------------------------------------------------------
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="ParentForm.aspx.vb" Inherits="TEST.ParentForm" %>
    <%@ Register TagPrefix="uc1" TagName="WebUserControl1"
    Src="WebUserControl1.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>ParentForm</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>
    <form id="Form1" method="post" runat="server">
    <P align="center">
    <uc1:WebUserControl1 id="WebUserControl11"
    runat="server"></uc1:WebUserControl1></P>
    </form>
    </body>
    </HTML>
    -----------------------------------------------------------

    CodeBehind:-----------------------------------------------------------
    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.HtmlControls

    Public Class ParentForm
    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

    '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

    Protected WithEvents WUC1 As WebUserControl1

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

    WUC1.LabelText = "TEST 1234"

    End Sub

    End Class

    -----------------------------------------------------------

    Thanks in advance.

    David
     
    David Webb, Dec 30, 2004
    #1
    1. Advertising

  2. "David Webb" <strangecargo> wrote in message
    news:...
    >I am having trouble setting the property to a Web User Control from the
    > Parent page.


    I don't see why that wouldn't work. What is the symptom? The value you set
    simply doesn't appear?

    John Saunders
     
    John Saunders, Dec 30, 2004
    #2
    1. Advertising

  3. David Webb

    David Webb Guest

    This is the first time that I have used this forum and I am only able to
    reply to the group.

    I get the following error:

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set
    to an instance of an object.

    Source Error:


    An unhandled exception was generated during the execution of the
    current web request. Information regarding the origin and location of the
    exception can be identified using the exception stack trace below.


    Stack Trace:


    [NullReferenceException: Object reference not set to an instance of an
    object.]
    TEST.ParentForm.Page_Load(Object sender, EventArgs e) in C:\Documents and
    Settings\dwebb\VSWebCache\INSPIRON2\TEST\ParentForm.aspx.vb:29
    System.Web.UI.Control.OnLoad(EventArgs e)
    System.Web.UI.Control.LoadRecursive()
    System.Web.UI.Page.ProcessRequestMain()





    "John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
    news:%...
    > "David Webb" <strangecargo> wrote in message
    > news:...
    > >I am having trouble setting the property to a Web User Control from the
    > > Parent page.

    >
    > I don't see why that wouldn't work. What is the symptom? The value you set
    > simply doesn't appear?
    >
    > John Saunders
    >
    >
     
    David Webb, Dec 30, 2004
    #3
  4. "David Webb" <strangecargo> wrote in message
    news:...
    > This is the first time that I have used this forum and I am only able to
    > reply to the group.
    >
    > I get the following error:
    >
    > Object reference not set to an instance of an object.


    How did you declare the user control in the parent page? This error means
    that the variable you are using to try to set the property in the control is
    not refencing the control (it is set to Nothing).

    John Saunders
     
    John Saunders, Dec 30, 2004
    #4
  5. David Webb

    David Webb Guest

    This is the html and codebehind for the parent page:

    Here's the Parent form: ParentForm.aspx
    HTML:-----------------------------------------------------------
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="ParentForm.aspx.vb" Inherits="TEST.ParentForm" %>
    <%@ Register TagPrefix="uc1" TagName="WebUserControl1"
    Src="WebUserControl1.ascx" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>ParentForm</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>
    <form id="Form1" method="post" runat="server">
    <P align="center">
    <uc1:WebUserControl1 id="WebUserControl11"
    runat="server"></uc1:WebUserControl1></P>
    </form>
    </body>
    </HTML>
    -----------------------------------------------------------

    CodeBehind:-----------------------------------------------------------
    Imports System
    Imports System.Web.UI
    Imports System.Web.UI.HtmlControls

    Public Class ParentForm
    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

    '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

    Protected WithEvents WUC1 As WebUserControl1

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

    WUC1.LabelText = "TEST 1234"

    End Sub

    End Class

    -----------------------------------------------------------



    "John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
    news:%...
    > "David Webb" <strangecargo> wrote in message
    > news:...
    > > This is the first time that I have used this forum and I am only able to
    > > reply to the group.
    > >
    > > I get the following error:
    > >
    > > Object reference not set to an instance of an object.

    >
    > How did you declare the user control in the parent page? This error means
    > that the variable you are using to try to set the property in the control

    is
    > not refencing the control (it is set to Nothing).
    >
    > John Saunders
    >
    >
     
    David Webb, Dec 30, 2004
    #5
  6. "David Webb" <strangecargo> wrote in message
    news:...
    > This is the html and codebehind for the parent page:
    >
    > Here's the Parent form: ParentForm.aspx
    > HTML:-----------------------------------------------------------
    > <%@ Page Language="vb" AutoEventWireup="false"
    > Codebehind="ParentForm.aspx.vb" Inherits="TEST.ParentForm" %>
    > <%@ Register TagPrefix="uc1" TagName="WebUserControl1"
    > Src="WebUserControl1.ascx" %>

    ....
    > <uc1:WebUserControl1 id="WebUserControl11"
    > runat="server"></uc1:WebUserControl1></P>

    ....
    > CodeBehind:-----------------------------------------------------------
    > Imports System
    > Imports System.Web.UI
    > Imports System.Web.UI.HtmlControls
    >
    > Public Class ParentForm
    > Inherits System.Web.UI.Page
    >

    ....
    >
    > Protected WithEvents WUC1 As WebUserControl1


    Either WUC1 has to be called WebUserControl11, or else you need to change
    the Id of your user control to "WUC1".

    John Saunders
     
    John Saunders, Dec 30, 2004
    #6
  7. David Webb

    David Webb Guest

    EXCELLENT! That solved the problem!

    I knew that there must be some small detail that I was overlooking.

    I greatly appreciate the help.

    Have a happy and safe New Year!

    Best Regards,
    David Webb

    "John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
    news:...
    > "David Webb" <strangecargo> wrote in message
    > news:...
    > > This is the html and codebehind for the parent page:
    > >
    > > Here's the Parent form: ParentForm.aspx
    > > HTML:-----------------------------------------------------------
    > > <%@ Page Language="vb" AutoEventWireup="false"
    > > Codebehind="ParentForm.aspx.vb" Inherits="TEST.ParentForm" %>
    > > <%@ Register TagPrefix="uc1" TagName="WebUserControl1"
    > > Src="WebUserControl1.ascx" %>

    > ...
    > > <uc1:WebUserControl1 id="WebUserControl11"
    > > runat="server"></uc1:WebUserControl1></P>

    > ...
    > > CodeBehind:-----------------------------------------------------------
    > > Imports System
    > > Imports System.Web.UI
    > > Imports System.Web.UI.HtmlControls
    > >
    > > Public Class ParentForm
    > > Inherits System.Web.UI.Page
    > >

    > ...
    > >
    > > Protected WithEvents WUC1 As WebUserControl1

    >
    > Either WUC1 has to be called WebUserControl11, or else you need to change
    > the Id of your user control to "WUC1".
    >
    > John Saunders
    >
    >
     
    David Webb, Dec 30, 2004
    #7
    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. Reny J Joseph Thuthikattu
    Replies:
    1
    Views:
    576
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Dec 30, 2004
  2. Roy
    Replies:
    1
    Views:
    880
    Scott Allen
    Feb 10, 2005
  3. Mark Rae
    Replies:
    3
    Views:
    842
  4. jamesd
    Replies:
    2
    Views:
    878
    jamesd
    Aug 15, 2006
  5. Michael

    Child user control accessing parent properties

    Michael, Jul 13, 2004, in forum: ASP .Net Building Controls
    Replies:
    9
    Views:
    493
    John Saunders
    Jul 30, 2004
Loading...

Share This Page