Accessing Control through class

Twister

Newcomer
Joined
Jul 11, 2003
Messages
6
Hello,

I've got a little Prob.:
Default.aspx - includes a Label (let's say Label1)
CustomClass.vb
Now i do
Code:
Dim CC as new CustomClass
CC.test()
In CustomClass, in Function test I want access to the Label which is in Default.aspx.
How can I do this? I do not want to do something like
Code:
CC.test(Label1)
..

Any ideas?
Thanks in advance! :)
 
What are you trying to do with the label? Set some value from the class? If so, you need to add a public event in your class, and raise the event. You will also need to add an event handler on your default.aspx page, so that when the class raises the event you can have the label respond:

in default.aspx, add the following code in your page load method (it needs to load each time, including post-backs:
Visual Basic:
AddHandler YourClassVariable.SetLabelText, AddressOf SetLabelTextFromClass

Also on default.aspx add the following method
Visual Basic:
Public Sub SetLabelTextFromClass(byval newtext as string)
  label1.text = newtext
  '...whatever else you want to do on the event firing
End Sub

Inside your class, put the following
Visual Basic:
Public Event SetLabelText(ByVal whatevertext As String)

Then from whatever method inside your class that updates the text:
Visual Basic:
'...inside the method
RaiseEvent SetLabelText("Text set by class method x")

Good Luck. If you need it to do something else, the best way to do so would be events. You could also use delegates but that is more complex and seems to have less functionality.

Brian
 
Hello cyclonebri,

thanks for your answer. I thought about a solution like this, but was not able to do it alone, but is there also a possibility to do this without the need to add code to default.aspx?
I recently found out, that I can access the session, request, response...etc. object by using httpcontext.current.session in a class. I'm looking for something similiar, like currentpage.findcontrol('Label1').Text = "abc" ?!

Thanks,
Twister
 
Is there a reason why you do not want to pass any parameters in? Would you not consider parameterisng the constructor and passing a reference to the page in on creation and storing that within the class for the remaining methods to act upon?
 
Yes, there is a reason. I use this class in many pages and with this method, i need to add code to each page. Now I want to change something in the SetLabelTextFromClass Sub and I have to do this in all pages... I hoped there would be an easy solution doing this only in the class....
 
Back
Top