Handling change event in Drop Down List

Discussion in 'ASP .Net Web Controls' started by Russ, Jul 15, 2004.

  1. Russ

    Russ Guest

    I have a composite control with a Microsoft.Web.UI.WebControls
    TabControl and Multipage controls, plus a bunch of text boxes and a
    couple of DropDownLists. Of course all the controls are created
    dynamically. I need to handle the event when the dropdown lists have
    their selected item changed, so I can pop up a dialog to allow editing
    of the item.

    I cannot get the SelectedIndexChanged event to fire. Here is what I
    am doing:

    When the DropDownList is created, I add an event handler like:

    DropDownList List = new DropDownList ();
    List.SelectedIndexChanged += new System.EventHandler(EditItem);

    And created a handler that is a member of the class that contains the
    composite control:

    private void EditItem (object sender, System.EventArgs e)
    {
    }

    The event never fires. I read that change events do not fire until
    postback time. This is not good, because I need an immediate response
    to the selection change. I tried adding List.AutoPostBack = true.
    This does cause post back, but the event still does not fire.

    Can anyone help?

    Thanks, Russ
    Russ, Jul 15, 2004
    #1
    1. Advertising

  2. Russ

    Dale Guest

    You can use the Attributes.Add method of your controls to add an "onchange"
    attribute and a value such as "ddlOnChange(this)". For example:

    myDropDown.Attributes.Add("onclick", "ddlOnChange(this)");

    Then add the javascript to your page either manually or using
    Page.RegisterClientScriptBlock.

    Search the framework documentation for "Client-Side Functionality in a
    Server Control" and the Attributes.Add method as well as the
    RegisterClientScriptBlock.

    You should be on your way!

    Dale


    "Russ" <> wrote in message
    news:eek:...
    > I have a composite control with a Microsoft.Web.UI.WebControls
    > TabControl and Multipage controls, plus a bunch of text boxes and a
    > couple of DropDownLists. Of course all the controls are created
    > dynamically. I need to handle the event when the dropdown lists have
    > their selected item changed, so I can pop up a dialog to allow editing
    > of the item.
    >
    > I cannot get the SelectedIndexChanged event to fire. Here is what I
    > am doing:
    >
    > When the DropDownList is created, I add an event handler like:
    >
    > DropDownList List = new DropDownList ();
    > List.SelectedIndexChanged += new System.EventHandler(EditItem);
    >
    > And created a handler that is a member of the class that contains the
    > composite control:
    >
    > private void EditItem (object sender, System.EventArgs e)
    > {
    > }
    >
    > The event never fires. I read that change events do not fire until
    > postback time. This is not good, because I need an immediate response
    > to the selection change. I tried adding List.AutoPostBack = true.
    > This does cause post back, but the event still does not fire.
    >
    > Can anyone help?
    >
    > Thanks, Russ
    Dale, Jul 15, 2004
    #2
    1. Advertising

  3. Russ

    Russ Guest

    Dale, Thank you for the information. It did work. I'm just getting
    back to this because been working on other things for a few days. I
    have a few questions and problems that you may be able to help with.

    In your example you are passing (this) to the OnChange handler. Why?
    It does not seem to make any difference if I pass it or nothing. I
    could find nothing in the suggested article you mentioned that
    explains this.

    I was able to get and show the selected value in a popup window as I
    wanted, but have not been able to figure out how to change the
    original value in the DDL when the user exits the popup with the OK
    button. Here is what I am doing.

    When the DDL is created, I add:

    DedList.Attributes.Add("onchange", "EditDeduction(this)");

    In the HTTP the following script is added:

    function EditDeduction ()
    {
    var DedData = event.srcElement.value;

    var WinSettings = "dialogHeight: 300px; dialogWidth: 400px;
    dialogTop: 150px; dialogLeft: 150px; edge: Sunken; center: No; help:
    No; resizable: No; status: No;"

    var ReturnVal = window.showModalDialog (
    "http://asrv/PayrollEntryClient/EditDeduction.aspx",
    DedData, WinSettings);

    if (ReturnVal != null)
    event.srcElement.value = ReturnVal;
    }

    It all works, except that instead of replacing the line to be modified
    in the DDL, a new empty line is added to the top. I have tried
    various things but cannot seem to be able to access the DDL directly
    with this script.

    Also, I see that when the event fires, the Page_Load routine for the
    popup window fires. Since it obviously does a postback (or page load
    would not fire), then I see no advantage in doing this with script.
    But if I could figure out how to capture the event data from the
    script call, or a pointer to the DDL, in the page load I could set up
    the window more easily, and handle the change to the DDL with an
    onclick event from the OK button. I have gone round and round on this
    but they are two separate worlds and I can find no way to get the data
    from the calling page into the page called by the DDL Attribute change
    event.

    It all comes back to my original question. What I NEED is for the
    system to work the way it appears to be documented. I can register a
    change event in code like:

    DedList.SelectedIndexChanged += new System.EventHandler(EditDed);
    DedList.AutoPostBack = true;

    The AutoPostBack is needed, according to documentation, because change
    events are not handled until postback, where they are handled one at a
    time in the order they are stored. But the event handler called
    EditDed never fires, even though changing the selection of the DDL
    does cause a postback. Clearly I am missing something here...

    If you can tell me how to make this work, preferably in code, not
    script, I would be very grateful.

    Thanks, Russ



    On Wed, 14 Jul 2004 21:12:37 -0500, "Dale"
    <> wrote:

    >You can use the Attributes.Add method of your controls to add an "onchange"
    >attribute and a value such as "ddlOnChange(this)". For example:
    >
    >myDropDown.Attributes.Add("onclick", "ddlOnChange(this)");
    >
    >Then add the javascript to your page either manually or using
    >Page.RegisterClientScriptBlock.
    >
    >Search the framework documentation for "Client-Side Functionality in a
    >Server Control" and the Attributes.Add method as well as the
    >RegisterClientScriptBlock.
    >
    >You should be on your way!
    >
    >Dale
    >
    >
    >"Russ" <> wrote in message
    >news:eek:...
    >> I have a composite control with a Microsoft.Web.UI.WebControls
    >> TabControl and Multipage controls, plus a bunch of text boxes and a
    >> couple of DropDownLists. Of course all the controls are created
    >> dynamically. I need to handle the event when the dropdown lists have
    >> their selected item changed, so I can pop up a dialog to allow editing
    >> of the item.
    >>
    >> I cannot get the SelectedIndexChanged event to fire. Here is what I
    >> am doing:
    >>
    >> When the DropDownList is created, I add an event handler like:
    >>
    >> DropDownList List = new DropDownList ();
    >> List.SelectedIndexChanged += new System.EventHandler(EditItem);
    >>
    >> And created a handler that is a member of the class that contains the
    >> composite control:
    >>
    >> private void EditItem (object sender, System.EventArgs e)
    >> {
    >> }
    >>
    >> The event never fires. I read that change events do not fire until
    >> postback time. This is not good, because I need an immediate response
    >> to the selection change. I tried adding List.AutoPostBack = true.
    >> This does cause post back, but the event still does not fire.
    >>
    >> Can anyone help?
    >>
    >> Thanks, Russ

    >
    Russ, Jul 19, 2004
    #3
    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. SirPoonga
    Replies:
    2
    Views:
    759
    Ben Strackany
    Jan 7, 2005
  2. weiwei
    Replies:
    0
    Views:
    1,014
    weiwei
    Jan 5, 2007
  3. msimmons
    Replies:
    0
    Views:
    472
    msimmons
    Jul 16, 2009
  4. Replies:
    5
    Views:
    267
  5. Replies:
    3
    Views:
    285
Loading...

Share This Page