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