digioz Posted July 23, 2006 Posted July 23, 2006 Hello Everyone, I am having a bit of a problem with a custom ASP.NET Control written in VB when I post the form and try to access the text of the custom control. Here is what I have: ' Custom Control Called ctlAddress with 1 label called lblAddress and one textbox called txtAddress '-------------------------------------------------------------------------------------------------- Public Class ctlAddress Inherits System.Web.UI.UserControl 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 Property Address() As String Get Return txtAddress.Text End Get Set(ByVal Value As String) txtAddress.Text = Value End Set End Property End Class ' HTML View of Actual ASP.NET Page that references the custom control above called WebForm1.aspx '----------------------------------------------------------------------------------------------- <%@ Register TagPrefix="uc1" TagName="ctlAddress" Src="controls/ctlAddress.ascx" %> <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>WebForm1</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="GridLayout"> <form id="Form1" method="post" runat="server"> <uc1:ctlAddress id="CtlAddress1" runat="server"></uc1:ctlAddress></DIV> <asp:Button id="btnSubmit" style="Z-INDEX: 105; LEFT: 320px; POSITION: absolute; TOP: 208px" runat="server" Text="Submit"></asp:Button> </form> </body> </HTML> ' Code behind for the WebForm1.aspx ----------------------------------- Public Class WebForm1 Inherits System.Web.UI.Page Dim myCtlAddress As ctlAddress 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 btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click myCtlAddress = New ctlAddress addr = myCtlAddress.Address lblOutput.Visible = True lblOutput.Text = addr End Sub End Class ' Error Message that appears is as follows '----------------------------------------- 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: Line 29: Public Property Address() As String Line 30: Get Line 31: Return txtAddress.Text Line 32: End Get Line 33: Set(ByVal Value As String) Source File: c:\inetpub\wwwroot\WebApplication1\controls\ctlAddress.ascx.vb Line: 31 Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] WebApplication1.ctlAddress.get_Address() in c:\inetpub\wwwroot\WebApplication1\controls\ctlAddress.ascx.vb:31 WebApplication1.WebForm1.btnSubmit_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx.vb:40 System.Web.UI.WebControls.Button.OnClick(EventArgs e) System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) System.Web.UI.Page.ProcessRequestMain() Does anyone know why the code above is generating the error? Thanks! Quote
digioz Posted July 24, 2006 Author Posted July 24, 2006 Never mind all. This fixed the problem: Protected WithEvents CtlAddress1 As ctlAddress Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.