Windows Vista Forums

Add Event Handler to dynamic DropDownList??????
  1. #1


    JP Guest

    Add Event Handler to dynamic DropDownList??????

    Add Event Handler to dynamic DropDownList

    I have a page that contains dynamically generated Dropdown List controls. On
    occasion, I want the dynamic dropdown list to perform an AutoPostback. Since
    the control is generated programmatically, I bind an event handler to the
    control before adding it to the controls collection like sew:
    ..
    ..
    ..
    DropDownList controlDropDown = new DropDownList();
    controlDropDown.ID = "drop" + drControls["controlName"].ToString();
    controlDropDown.AutoPostBack = true;
    controlDropDown.SelectedIndexChanged += new
    EventHandler(this.controlDropDown_SelectedIndexChanged);
    Page.Controls.Add(controlDropDown);
    ..
    ..
    ..
    protected void controlDropDown_SelectedIndexChanged(object sender, EventArgs
    e)
    {
    String a = “my answer” //my breakpoint
    }



    After everything is re-rendered on the post back (Yes I know you have to add
    the controls back to the collection on each postback) , the postback occurs
    and the controls are re-rendered, but the
    controlDropDown_SelectedIndexChanged event does not fire. Why???

    --
    JP
    ..NET Software Developer

      My System SpecsSystem Spec

  2. #2


    JP Guest

    RE: Add Event Handler to dynamic DropDownList??????

    I have solved the issue. DO NOT add the EventHandler when you create the
    DropDownList for the first time. Instead add the event handler to the control
    after the post back but BEFORE you add the DropDownList back to the controls
    collection.

    Makes since now since EventHandlers are not maintained in the Control State
    or View State. Then in the event method, use the Sender object to reference
    what was selected:

    String myValue = ((DropDownList)sender).SelectedValue;

    Maybe someone else will stumble across this post and find it helpful. It
    should work for any control that allows an AutoPostback.


    --
    JP
    ..NET Software Developer


    "JP" wrote:

    > Add Event Handler to dynamic DropDownList
    >
    > I have a page that contains dynamically generated Dropdown List controls. On
    > occasion, I want the dynamic dropdown list to perform an AutoPostback. Since
    > the control is generated programmatically, I bind an event handler to the
    > control before adding it to the controls collection like sew:
    > .
    > .
    > .
    > DropDownList controlDropDown = new DropDownList();
    > controlDropDown.ID = "drop" + drControls["controlName"].ToString();
    > controlDropDown.AutoPostBack = true;
    > controlDropDown.SelectedIndexChanged += new
    > EventHandler(this.controlDropDown_SelectedIndexChanged);
    > Page.Controls.Add(controlDropDown);
    > .
    > .
    > .
    > protected void controlDropDown_SelectedIndexChanged(object sender, EventArgs
    > e)
    > {
    > String a = “my answer” //my breakpoint
    > }
    >
    > After everything is re-rendered on the post back (Yes I know you have to add
    > the controls back to the collection on each postback) , the postback occurs
    > and the controls are re-rendered, but the
    > controlDropDown_SelectedIndexChanged event does not fire. Why???
    >
    > --
    > JP
    > .NET Software Developer

      My System SpecsSystem Spec

  3. #3


    Cor Ligthert[MVP] Guest

    Re: Add Event Handler to dynamic DropDownList??????

    JP,

    Put a panel between it and everything becomes much easier.

    Cor

    "JP" <JP@xxxxxx> wrote in message
    news:7EB3FCB2-3801-4380-82E4-3FF31970B5A3@xxxxxx

    > Add Event Handler to dynamic DropDownList
    >
    > I have a page that contains dynamically generated Dropdown List controls.
    > On
    > occasion, I want the dynamic dropdown list to perform an AutoPostback.
    > Since
    > the control is generated programmatically, I bind an event handler to the
    > control before adding it to the controls collection like sew:
    > .
    > .
    > .
    > DropDownList controlDropDown = new DropDownList();
    > controlDropDown.ID = "drop" + drControls["controlName"].ToString();
    > controlDropDown.AutoPostBack = true;
    > controlDropDown.SelectedIndexChanged += new
    > EventHandler(this.controlDropDown_SelectedIndexChanged);
    > Page.Controls.Add(controlDropDown);
    > .
    > .
    > .
    > protected void controlDropDown_SelectedIndexChanged(object sender,
    > EventArgs
    > e)
    > {
    > String a = “my answer” //my breakpoint
    > }
    >
    > After everything is re-rendered on the post back (Yes I know you have to
    > add
    > the controls back to the collection on each postback) , the postback
    > occurs
    > and the controls are re-rendered, but the
    > controlDropDown_SelectedIndexChanged event does not fire. Why???
    >
    > --
    > JP
    > .NET Software Developer

      My System SpecsSystem Spec

Add Event Handler to dynamic DropDownList?????? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView.DragDrop Event Handler Causes a DataError? (May be a Bug?!?!?!) Leon_Amirreza .NET General 3 27 Feb 2009
Questions about delegate and event handler Curious .NET General 3 23 Apr 2008
How to specify an event handler for the GridViewColumnHeader control in C#? wackyphill@yahoo.com Avalon 6 07 Nov 2006
Automatic Event Handler method for on the fly generated zammel control Hardikbill Indigo 2 18 Sep 2006
Automatic Event Handler method from zaml Events Hardikbill Indigo 0 15 Sep 2006