Asp.net vb user control raising event on calling page

I am trying to learn about user controls. I created a custom control with a text box and a button. What I would like to do is when I click the button in the user control, filling in the label on the aspx page. I understand that I just have a button on the page that uses some properties in the user control to get this information. But I would like to know how to do this using the user control button. The reason for this button is just an example .. a learning tool. if I can get this working, I would most likely put the thing in a user control that might require such a transfer of information. Anyway .. I found some C # examples that I was trying to get as vb .. but one day I started going into delegates and events ... well .. that made me turn to this post.

anyway .. hope someone can lend a hand.

+6
delegates user-controls
source share
3 answers

See if this tutorial helps. The best way to do this, of course, is to connect an event that is not as difficult as you think. The basics:

1. Define an event in your control class:

Public Event MyEvent as System.EventHandler

2. Lift the event into a subsection or function of your management class:

RaiseEvent MyEvent(Me, New EventArgs())

3. Manage the event on the page containing your control:

 Protected Sub MyControl_MyEvent(ByVal sender As Object, ByVal e As EventArgs) Handles MyControl.MyEvent 'Do stuff here End Sub 
+17
source share

I believe that you want your UserControl to output an event. This MSDN article explains how to raise events and there are examples of VB. Then your page can subscribe to an event that your UserControl raises and sets any values ​​that need to be set. It might be best to open some of your UserControl fields (i.e. Text fields, etc.) as public properties so that they are accessible from a signed control.

+2
source share

The problem is that if you force the usercontrol to depend on the parent page, it is no longer portable, and you defeated the goal of making it in usercontrol in the first place.

This is probably due to the fact that you are using delegates and something else in some examples - people are trying to remove the dependency on the parent page by allowing usercontrol to call the transfer method, instead of directly setting the label to the parent page.

If it's just for practice, you'd better click a button on the parent page to set the label in usercontrol, and not vice versa. The way you do it now is not a good design and should be avoided for real applications.

+1
source share

All Articles