Modify webpage controls in a contentplaceholder from a class.vb file
-
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 **** “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. Ron Brewer
-
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 **** “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. Ron Brewer
Hi, I have a question here,
TryCast(myPage.FindControl ("rightcolumn"), ContentPlaceHolder)
In this sentence is 'rightcolumn' your contentplaceholder id in your page? To access any control in your content form,use the below way.
Dim MyTxt As TextBox
MyTxt =CType(YOURContentPlaceHolderID.FindControl("YOURTextBoxID"), TextBox)Hope this helps.
-Manognya __________________________________________________ $ God gives what is best.Not what all you wish :)
-
Hi, I have a question here,
TryCast(myPage.FindControl ("rightcolumn"), ContentPlaceHolder)
In this sentence is 'rightcolumn' your contentplaceholder id in your page? To access any control in your content form,use the below way.
Dim MyTxt As TextBox
MyTxt =CType(YOURContentPlaceHolderID.FindControl("YOURTextBoxID"), TextBox)Hope this helps.
-Manognya __________________________________________________ $ God gives what is best.Not what all you wish :)
I tried your example and got a "rightcolumn not declared" error. If this is not what you meant tell me what I should use instead of the id of the contentplace holder and I will try that. I've seen some posts on the web indicating that I should be using "Master.FindControl" to get to the contentplaceholder where the controls are, but then I get a "Master not declared" error. That code works in the aspx.vb page, but not in the class.vb. I'm sure this has been attempted before, but so far my web surfing hasn't turned up any examples. It seems using a master page with contentplaceholders severely limits what you can do in a class file. Anyway, thank you for your interest and input. Ron Brewer
-
I tried your example and got a "rightcolumn not declared" error. If this is not what you meant tell me what I should use instead of the id of the contentplace holder and I will try that. I've seen some posts on the web indicating that I should be using "Master.FindControl" to get to the contentplaceholder where the controls are, but then I get a "Master not declared" error. That code works in the aspx.vb page, but not in the class.vb. I'm sure this has been attempted before, but so far my web surfing hasn't turned up any examples. It seems using a master page with contentplaceholders severely limits what you can do in a class file. Anyway, thank you for your interest and input. Ron Brewer
check this link.. http://stackoverflow.com/questions/3767965/how-to-access-an-asp-control-in-a-class-file[^] http://niitdeveloper.blogspot.com/2010/10/access-page-control-from-class-file-in.html[^] Its in c#.But hope it might help you.
-Manognya __________________________________________________ $ God gives what is best.Not what all you wish :)
-
check this link.. http://stackoverflow.com/questions/3767965/how-to-access-an-asp-control-in-a-class-file[^] http://niitdeveloper.blogspot.com/2010/10/access-page-control-from-class-file-in.html[^] Its in c#.But hope it might help you.
-Manognya __________________________________________________ $ God gives what is best.Not what all you wish :)
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 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
Great job!!
-Manognya __________________________________________________ $ God gives what is best.Not what all you wish :)