User Controls and event handling

I

Isaac Martinez

Hello all,

This may seem like a stupid question, however I've tried searching
pretty much everywhere with no success. I have an ASPX page with a few
user controls (ASCX). On One of the user controls is a drop downlist.

What I need to do is create an event handler on the ASPX page that will
handle a selected index changed event on this drop down on a ASCX user
control.

If anyone can send me a link to an article, or perhaps show me some
sample code it will be greatly appreciated.

Thanks again,
Isaac
 
J

John Saunders

Isaac Martinez said:
Hello all,

This may seem like a stupid question, however I've tried searching
pretty much everywhere with no success. I have an ASPX page with a few
user controls (ASCX). On One of the user controls is a drop downlist.

What I need to do is create an event handler on the ASPX page that will
handle a selected index changed event on this drop down on a ASCX user
control.

If anyone can send me a link to an article, or perhaps show me some
sample code it will be greatly appreciated.

Your user control needs to expose an event which the page can handle:

In YourControl.ascx.cs:

public event EventHandler SelectedIndexChanged;

protected void OnSelectedIndexChanged(EventArgs e)
{
if (SelectedIndexChanged != null)
{
SelectedIndexChanged(this, e);
}
}

protected void OnInit(EventArgs e)
{
...
dropDownList.SelectedIndexChanged += new
EventHandler(dropDownList_SelectedIndexChanged);
}

private void dropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
OnSelectedIndexChanged(e);
}


Your page can just use:
private void Page_Init(object sender, EventArgs e)
{
...
YourControl1.SelectedIndexChanged += new
EventHandler(YourControl_SelectedIndexChanged);
}

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top