04 January 2008

Raising events from user controls (.ascx)

Just like the DropDownList control has a SelectedIndexChanged event, you can create custom events for your custom user controls.

In the user control...

Public Event MyEvent As System.EventHandler

Protected Sub DoSomething()
    RaiseEvent MyEvent(Me, New EventArgs())
End Sub

And in the main page which contains the user control...

Protected Sub myControl_MyEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles myControl.MyEvent
    'Code goes here
End Sub

Quite good really. Now I just need to figure out how to cause a postback from the user control!

6 comments:

Evan Cutler said...

This was the most easiest way of setting an event. This is awesome.

The only thing I would put in, is a public Dim value as "datatype"

and change the value in the event that you want to fire back.

that way, when you call the event in the parent page, you can call the value you want.

THANK YOU!!! THANK YOU!!! THANK YOU!!!

Anonymous said...

use an ajax update panel to trigger the post back on the return of the event.

Anonymous said...

What about if my control is loaded dynamically to a placeholder.

Dim MyTestControl As Control = LoadControl("~/Controls/MyControl.ascx")
PlaceHolderControls.Controls.Add(MyTestControl)

Anonymous said...

Good for people to know.

Anonymous said...

Thank you very much for the code.
It was very helpful and easy to follow.

Unknown said...

Hi,Once the design specifications have been decided, it is best to start planning in Web Design Cochin the navigation structure and content of the Web pages. The process of Web design often involves creation of a mockup, which provides an idea of the layout of the Web page.Thanks..........