[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-08-10
7
Medium Priority
?
182 Views
Last Modified: 2010-04-23
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?
0
Comment
Question by:Eje2
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:DropZone
ID: 19679302
I'm not sure I understand.  Could you explain with more details, and post some code?

   -dZ.
0
 

Author Comment

by:Eje2
ID: 19690093
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
 
LVL 18

Expert Comment

by:DropZone
ID: 19690777
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Eje2
ID: 19691288
If we choose the public property method could you modify the code i sent you to make it function?
0
 

Author Comment

by:Eje2
ID: 19691926
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
 
LVL 18

Accepted Solution

by:
DropZone earned 1000 total points
ID: 19691970
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
 
LVL 18

Expert Comment

by:DropZone
ID: 19691980
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses
Course of the Month20 days, 3 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question