Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Modify webpage controls in a contentplaceholder from a class.vb file

Modify webpage controls in a contentplaceholder from a class.vb file

Scheduled Pinned Locked Moved ASP.NET
helpcsharpasp-netjson
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 8408064
    wrote on last edited by
    #1

    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

    M 1 Reply Last reply
    0
    • M Member 8408064

      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

      M Offline
      M Offline
      manognya kota
      wrote on last edited by
      #2

      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 :)

      M 1 Reply Last reply
      0
      • M manognya kota

        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 :)

        M Offline
        M Offline
        Member 8408064
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • M Member 8408064

          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

          M Offline
          M Offline
          manognya kota
          wrote on last edited by
          #4

          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 :)

          M 1 Reply Last reply
          0
          • M manognya kota

            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 :)

            M Offline
            M Offline
            Member 8408064
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • M Member 8408064

              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

              M Offline
              M Offline
              manognya kota
              wrote on last edited by
              #6

              Great job!!

              -Manognya __________________________________________________ $ God gives what is best.Not what all you wish :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups