views:

497

answers:

3

i'm trying to learn about user controls. I created a user control that has a textbox and a button. What i'd like to be able to do is when i click the button in the user control, populate a label in the aspx page. I understand that i could just have a button on the page that uses some properties on the user control to get that information.. but i'd like to know how to do it with the button the user control.. the reason for this is the button is just an example.. a learning tool. if i can get this working, i'd likely be putting thing in the user control that might require this kind of passing information. Anyway.. i've found some c# examples that i tried to get working as vb.. but once started getting into the delegates and events.. well.. it made me turn to this post.

anyway.. hopefully someone can lend a hand.

A: 

The problem is that if you make the usercontrol dependent on it's parent page, it's no longer portable, and you've defeated the purpose of making it into a usercontrol in the first place.

This is probably why you're running across delegates and whatnot in any examples - people are trying to remove the dependency on the parent page by allowing the usercontrol to call a passed-in method, instead of directly setting the label in the parent page.

If this is just for practice, you'd be better off pressing a button on the parent page to set a label in the usercontrol, rather than the other way around. The way you're doing it now is not a good design and should be avoided for real applications.

womp
one thing i was thinking about for a user control.. have a multi combo box that does autosuggest that would be used in several places.. the last item the user selects is the value i want. its fairly involoved and didn't want to copy and paste that much code or clutter. so was thinkng a user control would work nice. thing is, once that value is selected, i was hoping to not have to have user click a button to get teh value back to the page... within what you are talking about.. what approach would you take. thanks
jvcoach23
+1  A: 

See if this tutorial can help. The best way to do this is definitely to wire up an event, which isn't nearly as hard as you think. The basics are:

1. Define an event in your control class:

Public Event MyEvent as System.EventHandler

2. Raise the event in a sub or function of your control class:

RaiseEvent MyEvent(Me, New EventArgs())

3. Handle the event on the page that contains your control:

Protected Sub MyControl_MyEvent(ByVal sender As Object, ByVal e As EventArgs) Handles MyControl.MyEvent
    'Do stuff here
End Sub
tloflin
thanks for the info.. was wondering.. since you have such a nice and simple way of doing it.. how do you pass info back in the event.. or is it that when the event is fired (your 'Do stuff here) i'd grab the value from the proerty that is set on the user control
jvcoach23
If you want to pass info back in the event, you should change your "RaiseEvent" code to pass back something other than `new EventArgs()`. To keep it simple, you could even pass a string, like this: `RaiseEvent MyEvent(Me, MyString)`, and then change the second parameter of the `MyControl_MyEvent` sub on your page to be something like `ByVal s As String` instead of `ByVal e As EventArgs`. You can pass whatever you want in the event. I just used EventArgs because that is a common way of doing it (most people use their own class that inherits from EventArgs).
tloflin
great.. thanks for the responses...
jvcoach23
+1  A: 

I believe what you would want to do is to have your UserControl raise an event. This MSDN article explains how to raise events and has VB examples. Then your page can subscribe to the event that your UserControl raises and set whatever values you need to set. It might be best to expose some of your UserControl fields (i.e. TextBoxes, etc.) as public properties so they're accessible from the subscribed control.

CAbbott