By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,743 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,743 IT Pros & Developers. It's quick & easy.

Drop-down-list Client-side onchange stops Server-side SelectedIndexChanged event?

controlbox
P: 6
I have recently inplemented some drop-down lists (combos) on a page where one is dynamically populated according to the selection of another using the microsoft ajax extensions. I had to set AutoPostBack = true, and enable viewstate in order for the serverside events to fire, but once this was done, all seemed well - so far so good...

Also, when the page form is submitted, the Javascript validateForm() function is executed to perform client-side validation of the form.

My problem is that now AutoPostBack is enabled, it's possible for the form to be submitted as a result of a combo click, and validateForm() is no longer being called.

So... to overcome this, I added the following in the 'Render()' of the drop-down control...

this.Attributes["onchange"] = "javascript:return validateForm();";

...now when I click on a drop-down, the whole form is validated client-side to avoid the form being submitted in an invalid state.

All this works, but my server-side event .. SelectedIndexChanged on the combo is no longer firing when validateForm() is definately returning true.

It's almost as if the addition of the onchange attribute is somehow intefering with what ajax is doing under the covers.

Does anyone know how to effectively mix client-side and server-side (ajaxed) events on a combo such that the client-side validation can be performed, and only if this returns true, would we then want the (ajaxed) server-side event to go ahead and fire? I only seem to be able to do one or the other at the moment.

Cheers
Simon.
Nov 22 '07 #1
Share this Question
Share on Google+
7 Replies


controlbox
P: 6
further investigation reveals that ajax is adding the following into the onchange attribute on the control...

setTimeout('__doPostBack(\'ctl00$phMain$ctlVessel\ ',\'\')', 0)

...so, if I have already added...

javascript:return validateForm();

...into the onchange attribute, we end up with...

javascript:return validateForm();setTimeout('__doPostBack(\'ctl00$ph Main$ctlVessel\',\'\')', 0)

...so I think the 'return' statement is stopping the setTimeout() from executing, so the server-side event doesn't fire.

ideally, I want to be able to end up with the following in the onchange attribute...

javascript:if (validateForm()) setTimeout('__doPostBack(\'ctl00$phMain$ctlVessel\ ',\'\')', 0);

...but !!!! ajax always seems to add the semi-colon first, so all I end up with is...

javascript:if (validateForm()) ; setTimeout('__doPostBack(\'ctl00$phMain$ctlVessel\ ',\'\')', 0)

...which equates to ... if validateForm() then do nothing, then perform the postback anyway!!

In summary... I think I need a way to manipulate the contents on the onchange attribute AFTER ajax has added it's setTimeout(), but I do not know a way to do this - any ideas anyone???
Nov 22 '07 #2

controlbox
P: 6
SOLVED !!

for those of you who are interested...

render your own client-side javascript into the onchange like so...

control.Attributes["onchange"] = "javascript:if (validateForm('autopostback')==false) return false;"

...then ajax will add on it's postback mechanism, so we end up with...

"javascript:if (validateForm()==false) return false;setTimeout(....blah blah"

...so the setTimeout() will never execute if the form is invalid, but will as long as validateForm() returns true - exactly what I wanted.

cheers
Simon.
Nov 22 '07 #3

P: 34
if you need to change and already assigned event handler and also retain the previous function the you can do something like this:
Expand|Select|Wrap|Line Numbers
  1. var combobox = document.getElementById("comboboxid");
  2. var oldFunc = combobox.onchange;
  3. if (typeof oldFunc == "function") {
  4. oldFunc();
  5. //your code here
  6. } else {
  7. //your code here
  8. }
and in your case maybe something like this:
Expand|Select|Wrap|Line Numbers
  1. var oldFunc = this.Attributes["onchange"];
  2. if (typeof oldFunc == "function") {
  3. if (validateForm()) {
  4. oldFunc();
  5. }
  6. } else {
  7. return validateForm();
  8. }
by the way im curious about what this AutoPostBack is about. What is the use of it. is it an ASP thing
Nov 22 '07 #4

controlbox
P: 6
Yes, the standard asp.net combo (drop-down) control has a AutoPostBack property. If this is set to true, changing the selected item in the combo causes a postback to the server and the SelectedIndexChanged event fires.

(I'm pretty new to asp.net development, javascript and ajax myself, so I'm not 100% sure what is happening under the covers, but possibly the javascript:setTimeout(....) call on the onchange client-side event is a result of AutoPostBack = true rather than ajax ??

Many thanks for the alternative solution. Might be overkill for this circumstance as I'm happy for the setTimeout(...) call to just be tacked on the end of whatever I put in the onchange attribute, but... I can see how usefull that technique would be in other areas - many thanks.

Si.


if you need to change and already assigned event handler and also retain the previous function the you can do something like this:
Expand|Select|Wrap|Line Numbers
  1. var combobox = document.getElementById("comboboxid");
  2. var oldFunc = combobox.onchange;
  3. if (typeof oldFunc == "function") {
  4. oldFunc();
  5. //your code here
  6. } else {
  7. //your code here
  8. }
and in your case maybe something like this:
Expand|Select|Wrap|Line Numbers
  1. var oldFunc = this.Attributes["onchange"];
  2. if (typeof oldFunc == "function") {
  3. if (validateForm()) {
  4. oldFunc();
  5. }
  6. } else {
  7. return validateForm();
  8. }
by the way im curious about what this AutoPostBack is about. What is the use of it. is it an ASP thing
Nov 23 '07 #5

P: 1
Thanks so much Simon for this solution. I did come across the same problem as you did and tried the solution from the aspx page but of course, that didn't work and then I found your solution and tried it from the code-behind and ta-da...

Thank you so much,

Bhaskar Roy
May 13 '09 #6

P: 2
Thanks for the solution! The problem I'm having now is that dropdown should stick to the original one selected, not the one i newly selected. Has anyone else ran into this problem?
Nov 17 '11 #7

P: 2
This is what I came up with:

ASP.NET code-behind
Expand|Select|Wrap|Line Numbers
  1. ddl.ListBox.Attributes("onchange") = "if(ddl_OnChange('autopostback')==false) return false;"
JavaSript
Expand|Select|Wrap|Line Numbers
  1. var ddl = document.getElementById('<%= ddl.ClientID %>');
  2. var ddl = (ddl) ? ddl.selectedIndex : null;
  3.  
  4.  
  5. function ddl_OnChange(){
  6.     if(validateForm() == false){
  7.         if(ddl && ddlSelectedIndex != null) ddl.selectedIndex = ddlSelectedIndex;
  8.         return false;
  9.     }
  10. }
Nov 17 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.