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

D

David Webb

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
 
J

John Saunders

David Webb said:
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
 
D

David Webb

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()
 
J

John Saunders

David Webb said:
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
 
D

David Webb

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
 
J

John Saunders

David Webb said:
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
 
D

David Webb

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top