User Controls and event handling

Discussion in 'ASP .Net Building Controls' started by Isaac Martinez, Sep 9, 2003.

  1. 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
     
    Isaac Martinez, Sep 9, 2003
    #1
    1. Advertisements

  2. 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
     
    John Saunders, Sep 10, 2003
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.