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. Accessing Webform controls in class

Accessing Webform controls in class

Scheduled Pinned Locked Moved ASP.NET
csharpasp-net
4 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.
  • N Offline
    N Offline
    Nilesh Hapse
    wrote on last edited by
    #1

    hello everybody, I'm creating a web applocation in ASP.Net using C# as page behind code. I've one form named WebForm1.aspx and there is a textbox control TextBox1 on it. Then I added a class Class1.cs in the application. There are some functions in the class. Now i want to acces the TextBox1 control of WebForm1.aspx in my class Class1.cs . I think we can not acces Webform controls inside a class. Is there any way to acces the control in the class. Thanks in advance

    J 1 Reply Last reply
    0
    • N Nilesh Hapse

      hello everybody, I'm creating a web applocation in ASP.Net using C# as page behind code. I've one form named WebForm1.aspx and there is a textbox control TextBox1 on it. Then I added a class Class1.cs in the application. There are some functions in the class. Now i want to acces the TextBox1 control of WebForm1.aspx in my class Class1.cs . I think we can not acces Webform controls inside a class. Is there any way to acces the control in the class. Thanks in advance

      J Offline
      J Offline
      Jesse Squire
      wrote on last edited by
      #2

      Due to the protection level of the textbox when it is declared on the webform, you cannot directly access it publicly. It is, however, possible to call a method of Class1 passing the textbox as a parameter. Are you sure that Class1 should have knowledge of the individual elements of the interface? From the limited information provided, it sounds like Class1 may be violating the abstraction. Of course, I could also just be jumping to conclusions. :~ Hope that helps a bit. :) --Jesse

      N 1 Reply Last reply
      0
      • J Jesse Squire

        Due to the protection level of the textbox when it is declared on the webform, you cannot directly access it publicly. It is, however, possible to call a method of Class1 passing the textbox as a parameter. Are you sure that Class1 should have knowledge of the individual elements of the interface? From the limited information provided, it sounds like Class1 may be violating the abstraction. Of course, I could also just be jumping to conclusions. :~ Hope that helps a bit. :) --Jesse

        N Offline
        N Offline
        Nilesh Hapse
        wrote on last edited by
        #3

        hi Jesse, I know we can pass textbox as parameter to the class :). But i want to directly access the control's .Text property from the class. Like in VB 6.0 we can access the properties of a control on a form from a class module. Can i do that in ASP.Net?

        J 1 Reply Last reply
        0
        • N Nilesh Hapse

          hi Jesse, I know we can pass textbox as parameter to the class :). But i want to directly access the control's .Text property from the class. Like in VB 6.0 we can access the properties of a control on a form from a class module. Can i do that in ASP.Net?

          J Offline
          J Offline
          Jesse Squire
          wrote on last edited by
          #4

          I would counsel that using a pseudo-object oriented language like VB6 as a model of good practice can get you into trouble. But if you sure that is the approach you want.... Simply change the access level decorator for each textbox from protected to public, and they will look to Class1 as if they were public properties on the instance of the page. One caveat that I want to mention, after changing the access level, you'll probably want to avoid using the VS.NET designer. Unless I am remembering incorrectly, it will rewrite the code each time you use it, changing the access level back to protected. Hope that helps. :) --Jesse

          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