Events in server controls

Discussion in 'ASP .Net Building Controls' started by I am Sam, Feb 16, 2008.

  1. I am Sam

    I am Sam Guest

    I need to write a composite control where a click event in the
    parent(container) class fires an event in a child control. How is this
    possible? ie clicking a button in the parent control performs an action on a
    child control.
     
    I am Sam, Feb 16, 2008
    #1
    1. Advertisements

  2. I am Sam

    Teemu Keiski Guest

    Hi,

    you could access the child control in button's click event? Or if it's
    something internal in the child control, it should be informed somehow that
    the Click occurred, maybe it could wire up to an event of the parent control
    which the parent exposes publicly?
     
    Teemu Keiski, Mar 16, 2008
    #2
    1. Advertisements

  3. I am Sam

    I am Sam Guest

    The solution is very simple really. You can for example write the following:
    protected override void CreateChildControls()
    {
    ImageButton someImageButton=new ImageButton();
    someImageButton.Click += new ImageClickEventHandler(ImageButton_Click);
    this.Controls.Add(someImageButton);
    }

    protected void ImageButton_Click(object sender, ImageClickEventArgs e)
    {
    do something..;
    }

    within your custom control. You only want to bubble events when you want to
    expose your event in the containing page.
     
    I am Sam, Mar 16, 2008
    #3
    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.