Is it possible to artificially trigger dropdownlist event on server?

Discussion in 'ASP .Net Web Controls' started by Eran Amitai, Jan 6, 2004.

  1. Eran Amitai

    Eran Amitai Guest

    I have a dropdownlist control that doesn't automatically post back. When
    certain entries are selected, however, I need to go to the server. I
    implemented client script that catches the SelectChanged event, checks the
    value selected, and if it is one of two special values, explicitly posts
    back to the server. So far so good.

    My problem is that one of the two special entries is the first entry,
    initially selected when the page is displayed. On the client side, if you
    drop down the list and reselect the same entry no event is generated. If,
    however, the user selects a different entry and then drops it down again and
    selects back the first entry, a client event is created, which I happily
    trap and post back to the server.

    Alas, on the server side no event is generated because the droplist control
    thinks the value hasn't changed. Since it is not aware of the transition
    that occured on the client side it thinks nothing changed. I do not get an
    opportunity to process my special value.

    Is there any way I can force the droplist control to fire an event? Can I
    tamper with the view state just after it's loaded but before the control's
    LoadPostData function is called so as to fool the list control? (I saw the
    control's ViewState property is protected so I can't access it.)

    Obviously I can implement a custom control that derives from the
    dropdownlist control, but that seems a bit extreme to me. I'm looking for an
    easier solution.

    Any advice would be appreciated. Thanks.
    Eran Amitai, Jan 6, 2004
    #1
    1. Advertising

  2. Eran Amitai

    Mythran Guest

    "Eran Amitai" <> wrote in message
    news:%...
    > I have a dropdownlist control that doesn't automatically post back. When
    > certain entries are selected, however, I need to go to the server. I
    > implemented client script that catches the SelectChanged event, checks the
    > value selected, and if it is one of two special values, explicitly posts
    > back to the server. So far so good.
    >


    Javascript:
    __doPostBack('controlnamehere', '');

    VBScript:
    window.execScript "__doPostBack('controlnamehere', '');", "JavaScript"

    hope this helps :)

    Mythran
    Mythran, Jan 6, 2004
    #2
    1. Advertising

  3. Eran Amitai

    Alvin Bruney Guest

    Your easiest solution is to add an intial option "Select an option" this
    preceeds your 2 special entries. So you have 3 entries in the dropdown, 2
    valid and 1 instructional. That's the way most folk get around the selection
    change event problem. You'd have to add code to ignore the "select an
    option" if a user selects another option and changes back to "select an
    option" because you wouldn't want to submit that invalid option. This way is
    cheap and easy and works well.

    --
    Regards,
    Alvin Bruney
    Got tidbits? Get it here...
    http://tinyurl.com/2bz4t
    "Eran Amitai" <> wrote in message
    news:%...
    > I have a dropdownlist control that doesn't automatically post back. When
    > certain entries are selected, however, I need to go to the server. I
    > implemented client script that catches the SelectChanged event, checks the
    > value selected, and if it is one of two special values, explicitly posts
    > back to the server. So far so good.
    >
    > My problem is that one of the two special entries is the first entry,
    > initially selected when the page is displayed. On the client side, if you
    > drop down the list and reselect the same entry no event is generated. If,
    > however, the user selects a different entry and then drops it down again

    and
    > selects back the first entry, a client event is created, which I happily
    > trap and post back to the server.
    >
    > Alas, on the server side no event is generated because the droplist

    control
    > thinks the value hasn't changed. Since it is not aware of the transition
    > that occured on the client side it thinks nothing changed. I do not get an
    > opportunity to process my special value.
    >
    > Is there any way I can force the droplist control to fire an event? Can I
    > tamper with the view state just after it's loaded but before the control's
    > LoadPostData function is called so as to fool the list control? (I saw the
    > control's ViewState property is protected so I can't access it.)
    >
    > Obviously I can implement a custom control that derives from the
    > dropdownlist control, but that seems a bit extreme to me. I'm looking for

    an
    > easier solution.
    >
    > Any advice would be appreciated. Thanks.
    >
    >
    Alvin Bruney, Jan 6, 2004
    #3
  4. Eran Amitai

    Eran Amitai Guest

    Thanks Alvin and Mythran for your responses. Mythran, I know how to post
    back to the server, my problem is that no change event is generated on the
    server by the dropdownlist server control because the entry supposedly never
    changed.

    I did some more research on this and it seems that Alvin's proposal is the
    only reasonable solution. That's what I'll go with. Thanks again.


    "Alvin Bruney" <vapor at steaming post office> wrote in message
    news:...
    > Your easiest solution is to add an intial option "Select an option" this
    > preceeds your 2 special entries. So you have 3 entries in the dropdown, 2
    > valid and 1 instructional. That's the way most folk get around the

    selection
    > change event problem. You'd have to add code to ignore the "select an
    > option" if a user selects another option and changes back to "select an
    > option" because you wouldn't want to submit that invalid option. This way

    is
    > cheap and easy and works well.
    >
    > --
    > Regards,
    > Alvin Bruney
    > Got tidbits? Get it here...
    > http://tinyurl.com/2bz4t
    > "Eran Amitai" <> wrote in message
    > news:%...
    > > I have a dropdownlist control that doesn't automatically post back. When
    > > certain entries are selected, however, I need to go to the server. I
    > > implemented client script that catches the SelectChanged event, checks

    the
    > > value selected, and if it is one of two special values, explicitly posts
    > > back to the server. So far so good.
    > >
    > > My problem is that one of the two special entries is the first entry,
    > > initially selected when the page is displayed. On the client side, if

    you
    > > drop down the list and reselect the same entry no event is generated.

    If,
    > > however, the user selects a different entry and then drops it down again

    > and
    > > selects back the first entry, a client event is created, which I happily
    > > trap and post back to the server.
    > >
    > > Alas, on the server side no event is generated because the droplist

    > control
    > > thinks the value hasn't changed. Since it is not aware of the transition
    > > that occured on the client side it thinks nothing changed. I do not get

    an
    > > opportunity to process my special value.
    > >
    > > Is there any way I can force the droplist control to fire an event? Can

    I
    > > tamper with the view state just after it's loaded but before the

    control's
    > > LoadPostData function is called so as to fool the list control? (I saw

    the
    > > control's ViewState property is protected so I can't access it.)
    > >
    > > Obviously I can implement a custom control that derives from the
    > > dropdownlist control, but that seems a bit extreme to me. I'm looking

    for
    > an
    > > easier solution.
    > >
    > > Any advice would be appreciated. Thanks.
    > >
    > >

    >
    >
    Eran Amitai, Jan 7, 2004
    #4
  5. Eran Amitai

    Eran Amitai Guest

    I found my solution!! It may not work in all cases and is a little dirty but it certainly works in my case.

    As I said, I have client script that catches the droplist selection event. Most selections are benign but two special entries (in my case the first and last entries) trigger postbacks to the server. However, since the first entry is also the one selected when the page is displayed, the droplist server handler processing the postback doesn't fire the change event.

    What I did was add a line of code to my client that if the first entry is selected, change the value to yet another special value, one that doesn't exist in the list. The (client-side) function looks like:
    var Inx = selectedIndex;
    if (options[Inx].value == -9998) // First row special value - initially selected
    options[Inx].value = -9997; // Change to fake value not present in list
    if (options[Inx].value <= -9997) // One of: -9997, -9998, -9999. Requires postback
    call doPostBack function

    I am not familiar with the inner workings of the drop list control. It must be "surprised" that the value returned from the form is not part of the list. Then again, people can do funny things in client scripts (such as add options) so it has to be at least ready for something like that. In any case, it fires the change event as it recognizes the value returned as different to the one selected on render. In the even itself the droplist control returns the selected index as zero, which is just perfect for me. (Surprisingly, the SelectedValue property is set to the -9998 and not the actual returned value of -9997 which is what the form returns. It's like it decides the value is invalid and discards it.)

    Thanks to all posters. It's been educating.


    "Alvin Bruney" <vapor at steaming post office> wrote in message news:...
    > Your easiest solution is to add an intial option "Select an option" this
    > preceeds your 2 special entries. So you have 3 entries in the dropdown, 2
    > valid and 1 instructional. That's the way most folk get around the selection
    > change event problem. You'd have to add code to ignore the "select an
    > option" if a user selects another option and changes back to "select an
    > option" because you wouldn't want to submit that invalid option. This way is
    > cheap and easy and works well.
    >
    > --
    > Regards,
    > Alvin Bruney
    > Got tidbits? Get it here...
    > http://tinyurl.com/2bz4t
    > "Eran Amitai" <> wrote in message
    > news:%...
    > > I have a dropdownlist control that doesn't automatically post back. When
    > > certain entries are selected, however, I need to go to the server. I
    > > implemented client script that catches the SelectChanged event, checks the
    > > value selected, and if it is one of two special values, explicitly posts
    > > back to the server. So far so good.
    > >
    > > My problem is that one of the two special entries is the first entry,
    > > initially selected when the page is displayed. On the client side, if you
    > > drop down the list and reselect the same entry no event is generated. If,
    > > however, the user selects a different entry and then drops it down again

    > and
    > > selects back the first entry, a client event is created, which I happily
    > > trap and post back to the server.
    > >
    > > Alas, on the server side no event is generated because the droplist

    > control
    > > thinks the value hasn't changed. Since it is not aware of the transition
    > > that occured on the client side it thinks nothing changed. I do not get an
    > > opportunity to process my special value.
    > >
    > > Is there any way I can force the droplist control to fire an event? Can I
    > > tamper with the view state just after it's loaded but before the control's
    > > LoadPostData function is called so as to fool the list control? (I saw the
    > > control's ViewState property is protected so I can't access it.)
    > >
    > > Obviously I can implement a custom control that derives from the
    > > dropdownlist control, but that seems a bit extreme to me. I'm looking for

    > an
    > > easier solution.
    > >
    > > Any advice would be appreciated. Thanks.
    > >
    > >

    >
    >
    Eran Amitai, Jan 7, 2004
    #5
    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. Irfi
    Replies:
    0
    Views:
    610
  2. Irfi
    Replies:
    0
    Views:
    421
  3. Morgan Cheng
    Replies:
    3
    Views:
    1,705
    John Timney \(MVP\)
    Apr 30, 2007
  4. PJ6
    Replies:
    3
    Views:
    436
  5. Anthony Papillion

    TkInter bind() event is not firing event trigger

    Anthony Papillion, Jun 23, 2010, in forum: Python
    Replies:
    3
    Views:
    1,654
    rantingrick
    Jun 23, 2010
Loading...

Share This Page