Issue in VS 2005 ASP.Net Web App referencing controls between clas

Discussion in 'ASP .Net Web Controls' started by George Kenyon, Mar 24, 2009.

  1. Hi,

    I am rewriting a Windows Application which has many vb classes containing
    code which references controls such as Label which are defined in the class
    where the form design is, also the code in the other classes dynamically add
    or change properties of controls in the design class.

    When I try to duplicate this in my ASP.Net 2 Web Application in Visual
    Studio 2005 the Web Page controls such as Label are not accessible in the
    other classes and a NullReferenceError occurs at run time.

    I have created a test project with two classes in the same page code behind
    and the string MyString in the page class is available in the other class but
    the Label Label1 isn't and gets the NullReferenceError.

    This is the code in the test project:

    Public Class DefaultMine
    Inherits System.Web.UI.Page
    Public MyString As String = "This is the base class string."
    Public Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    End Sub
    Public Sub Button1_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Button1.Click
    Dim ac As New AnotherClass
    Label1.Text = "Label test in Button Click"
    ac.TestSub()
    End Sub
    End Class

    Public Class AnotherClass : Inherits DefaultMine
    Public Sub TestSub()
    MyBase.MyString = "String test in Class AnotherClass"
    MyBase.Label1.Text = "Label test in Class AnotherClass"
    End Sub
    End Class

    The designer.vb declaration for the label is:

    Public WithEvents Label1 As Global.System.Web.UI.WebControls.Label

    When I step through the program the AnotherClass Class code sees the value
    of the string MyString declared in the page class and changes to the new
    value but the Label comes in in with no value and gets the error.

    Does anyone know how to resolve this because I have tried everything I can
    think of and looked in many forums for a solution.

    Your help would be much appreciated.

    --
    George
     
    George Kenyon, Mar 24, 2009
    #1
    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. ad
    Replies:
    5
    Views:
    1,622
  2. =?Utf-8?B?RGlmZmlkZW50?=
    Replies:
    5
    Views:
    481
    Bruce Barker
    Jan 13, 2006
  3. Andy
    Replies:
    1
    Views:
    506
  4. -
    Replies:
    1
    Views:
    337
    Kevin McMurtrie
    Jun 13, 2005
  5. scanner clas

    , Sep 18, 2005, in forum: Java
    Replies:
    8
    Views:
    5,348
    ChrisWSU
    Sep 19, 2005
Loading...

Share This Page