How to refer to webcontrol in vb file inherited by the webpage

I have a webapplication with code written in VB.NET 2003. The application contains several webpages. Some code is more or less common for all pages. This code is gathered in a vb file. All pages inherits this vb file. My problem is that I can't refer to controls common to all webpages in the vb file. As soon as the code includes reference to a control I have to copy the same code to all pages. Is there a way to put this type of code in the vb file instead?
Eje2Asked:
Who is Participating?
 
DropZoneConnect With a Mentor Commented:
Public Class WantedCommonCode
  Inherits System.Web.UI.Page

  ' This will hold the reference internally
  Private m_TextBoxRef As TextBox

  ' Define a public property for the reference
  Public Property TextBoxReference() As TextBox
    Get
        Return m_TextBoxRef
    End Get

    Set(ByRef Ctrl As TextBox)
        m_TextBoxRef = Ctrl
    End Set
  End Property

  Protected Sub RunSomeCode()
    '...

      ' Call the method that will set the message
      TextBoxWrite("MessageToUser", Color.Red)

      '...
  End Sub

  Protected Sub TextBoxWrite(ByVal message As String, ByVal color As Drawing.Color)
    ' Set the properties if we have a reference defined
      ' We indirectly set the properties using the reference
      If (m_TextBoxRef <> Nothing) Then
            With m_TextBoxRef
                  .Text      = message
                  .ForeColor = color
            End With
      End If
  End Sub
End Class


Public Class WebForm2
  ' ...
  ' Nothing changed here
  ' ...

  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

      ' We initialize the reference to the Textbox:
      Me.TextBoxReference = TextBox1
  End Sub
'
'No code for sub TextBox1_TextWrite
'
End Class
0
 
DropZoneCommented:
I'm not sure I understand.  Could you explain with more details, and post some code?

   -dZ.
0
 
Eje2Author Commented:
One exemple: My application has more than 20 webforms. On many I have a textbox to show a message to the user. Now I have to write the same sub TextBox1_TextWrite on every page
  Private Sub TextBox1_TextWrite(ByVal message As String, ByVal color As Drawing.Color)
    With TextBox1
      .Text = message
      .ForeColor = color
    End With
  End Sub

I would like to write this code only once in WantedCommonCode as shown here:

Public Class WantedCommonCode
  Inherits System.Web.UI.Page
  Protected Sub RunSomeCode()
    '...
    TextBox1_TextWrite("MessageToUser", Color.Red)
    '...
  End Sub
  Protected Sub TextBox1_TextWrite(ByVal message As String, ByVal color As Drawing.Color)
    With TextBox1
      .Text = message
      .ForeColor = color
    End With
  End Sub
End Class

Public Class WebForm2
  Inherits WantedCommonCode
#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 TextBox1 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
'
'No code for sub TextBox1_TextWrite
'
End Class
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
DropZoneCommented:
I see.  Here's the thing:  the IDE does some behind-the-scenes magic to maintain a reference between the controls of the page and the code-behind, but this is not strictly part of the framework; this is just a nice bonus provided by VisualStudio.  When you put that code in your separate module, the module does not have that magical relationship with the controls of the page.

There are a couple of ways you could fix this, but they all require that you acquire a reference programmatically to the TextBox.  You can do this by using FindControl(), but you will need to ensure that all pages have the same TextBox control with the same name.  This could be a maintenance problem.

Another approach is to *give* this reference to your module, since the page already has it by magic.  For example, the WantedCommonCode could contain a public property for this reference and you can set it from the Page_Init() method on every page.

Does this make sense?

    -dz.
0
 
Eje2Author Commented:
If we choose the public property method could you modify the code i sent you to make it function?
0
 
Eje2Author Commented:
Ok. It functions! I did like this.

Public Class WantedCommonCode

  Inherits System.Web.UI.Page

  Private pTextBox1P As System.Web.UI.WebControls.TextBox

  Protected Sub RunSomeCode()
    '...
    TextBox1_TextWrite("MessageToUser", Color.Red)
    '...
  End Sub

  Protected Sub TextBox1_TextWrite(ByVal message As String, ByVal color As Drawing.Color)
    With TextBox1P
      .Text = message
      .ForeColor = color
    End With
  End Sub

  Public Property TextBox1P() As System.Web.UI.WebControls.TextBox
    Get
      Return pTextBox1P
    End Get
    Set(ByVal Value As System.Web.UI.WebControls.TextBox)
      pTextBox1P = Value
    End Set
  End Property

End Class

Public Class WebForm2
  Inherits WantedCommonCode

#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 TextBox1 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
    TextBox1P = TextBox1
    RunSomeCode()
  End Sub

End Class
0
 
DropZoneCommented:
Sorry, I posted at the same time.  Your code is basically the same as mine.  The only thing I would suggest is to check if the reference to pTextBox1P is defined prior to using it in the TextBox1_TextWrite() method.

    -dZ.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.