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. Advertising

  2. "Isaac Martinez" <> wrote in message
    news:...
    > 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
    --
    John Saunders
    Internet Engineer
     
    John Saunders, Sep 10, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bruce barker

    Re: event handling with user controls

    bruce barker, Aug 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,600
  2. Scott Allen

    Re: event handling with user controls

    Scott Allen, Aug 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    331
    Scott Allen
    Aug 2, 2004
  3. Nick Stansbury

    web user controls / event handling

    Nick Stansbury, Aug 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    460
    Steve Caliendo
    Aug 9, 2004
  4. manu_srinivasa
    Replies:
    0
    Views:
    410
    manu_srinivasa
    Nov 24, 2004
  5. Majeti Lakshmi Sreenivasulu

    Dynamic User Controls Event Handling Problem

    Majeti Lakshmi Sreenivasulu, Nov 11, 2003, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    157
    Majeti Lakshmi Sreenivasulu
    Nov 11, 2003
Loading...

Share This Page