Is it possible to artificially trigger dropdownlist event on server?

E

Eran Amitai

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

Mythran

Eran Amitai said:
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
 
A

Alvin Bruney

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

Eran Amitai

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

Eran Amitai

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top