Accessing Webform controls in class
-
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
-
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
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
-
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
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?
-
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?
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
topublic
, 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 toprotected
. Hope that helps. :) --Jesse