Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I've got a little Prob.:

Default.aspx - includes a Label (let's say Label1)

CustomClass.vb

Now i do

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

CC.test(Label1)

..

 

Any ideas?

Thanks in advance! :)

Posted

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:

AddHandler YourClassVariable.SetLabelText, AddressOf SetLabelTextFromClass

 

Also on default.aspx add the following method

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

Public Event SetLabelText(ByVal whatevertext As String)

 

Then from whatever method inside your class that updates the text:

'...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

Posted

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

Posted
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....

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...