0

I'm using VB.NET and ASP.NET in VS2005 to create a web application that has some very similar code in five different webpages. This code enables/disables various controls depending on the actions allowed in the current page. I would like to consolidate this code into a subroutine in a class.vb file and call it from the aspx.vb files.

I’m using a Master Page with four contentpageholders. Only one has the controls in question, but herein lies my problem. I can access the controls on webpages not using the Master Page, but can’t make the code work with the contentpageholder.

Here is the code in the class file:

Public Sub SetControls(ByVal Controltype As String, ByVal StartCtrl As Integer, ByVal StopCtrl As Integer, ByVal SetValue As Boolean)
Dim myPage As Page
Dim intIndex As Integer
Dim objPlaceHolder As ContentPlaceHolder

myPage = TryCast(HttpContext.Current.Handler, Page)
objPlaceHolder = TryCast(myPage.FindControl("rightcolumn"), ContentPlaceHolder)

**** If Not objPlaceHolder Is Nothing Then **** Always “Nothing”
Select Case Controltype
Case "TextBox"
Dim txtTextBox As TextBox

For intIndex = StartCtrl To StopCtrl

txtTextBox = TryCast(myPage.FindControl("TextBox" & intIndex), TextBox)

If Not txtTextBox Is Nothing Then
txtTextBox.ReadOnly = SetValue
Else
Exit For
End If
Next
End Select
End If
End Sub
End Class

And I call the procedure like this:

Sub LockControls()

Dim objMiscUtilities As New MiscUtilities

objMiscUtilities.SetControls("TextBox", 2, 20, False)

End Sub

Whenever I test the program the “objPlaceHolder” is always nothing, so the rest of the code is bypassed.

Any help will be greatly appreciated.

Thanks in advance,

Ron Brewer

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by rbrewer_phca
0

I'm using VB.NET and ASP.NET in VS2005 to create a web application that has some very similar code in five different webpages. This code enables/disables various controls depending on the actions allowed in the current page. I would like to consolidate this code into a subroutine in a class.vb file and call it from the aspx.vb files.

I’m using a Master Page with four contentpageholders. Only one has the controls in question, but herein lies my problem. I can access the controls on webpages not using the Master Page, but can’t make the code work with the contentpageholder.

Here is the code in the class file:

Public Sub SetControls(ByVal Controltype As String, ByVal StartCtrl As Integer, ByVal StopCtrl As Integer, ByVal SetValue As Boolean)
Dim myPage As Page
Dim intIndex As Integer
Dim objPlaceHolder As ContentPlaceHolder

myPage = TryCast(HttpContext.Current.Handler, Page)
objPlaceHolder = TryCast(myPage.FindControl("rightcolumn"), ContentPlaceHolder)

**** If Not objPlaceHolder Is Nothing Then **** Always “Nothing”
Select Case Controltype
Case "TextBox"
Dim txtTextBox As TextBox

For intIndex = StartCtrl To StopCtrl

txtTextBox = TryCast(myPage.FindControl("TextBox" & intIndex), TextBox)

If Not txtTextBox Is Nothing Then
txtTextBox.ReadOnly = SetValue
Else
Exit For
End If
Next
End Select
End If
End Sub
End Class

And I call the procedure like this:

Sub LockControls()

Dim objMiscUtilities As New MiscUtilities

objMiscUtilities.SetControls("TextBox", 2, 20, False)

End Sub

Whenever I test the program the “objPlaceHolder” is always nothing, so the rest of the code is bypassed.

Any help will be greatly appreciated.

Thanks in advance,

Ron Brewer

This problem has been resolved (with a lot of help from my friends) and I thought it would be worth while to post the solution to help any others who might be interested. It only took two relatively minor changes to make the code work. Here they are:

Original Procedure declaration
Public Sub SetControls(ByVal Controltype As String, ByVal StartCtrl As Integer, ByVal StopCtrl As Integer, ByVal SetValue As Boolean)

Original call to procedure
objMiscUtilities.SetControls("TextBox", 2, 20, False)

Corrected Procedure declaration
Public Sub SetControls(ByVal Controltype As String, ByVal StartCtrl As Integer, ByVal StopCtrl As Integer, ByVal SetValue As Boolean, ByVal objPlaceHolder As ContentPlaceHolder)

Corrected call to procedure
objMiscUtilities.SetControls("TextBox", 2, 20, False, Master.FindControl("rightcolumn"))


Ron Brewer

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.