473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

controlbox
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
7 22312
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
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
andho
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
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
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
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
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

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

Similar topics

2
by: Ryan | last post by:
Just a quicky about temporarary tables. If using QA, when you create a temporary table, it gets dropped if you close the query. Otherwise you need to state 'DROP TABLE myTable' so that you can...
1
by: riehe | last post by:
Dear all I want to drop a node from a nodegroup. Can someone confirm if its enough to redistribute a node group with a partition map which doesn't include the node to be dropped? I can't see...
1
by: Ian Dobson | last post by:
Hi, I need to drop a schema from a database but it has 400 tables in it. Is there an easy way to do it other than drop table schema1.table1 drop table schema1.table2 etc.. to 400 and then drop...
10
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
0
by: Lauren Quantrell | last post by:
I'm trying to drop a file from Windows Explorer (or desktop, etc.) onto a field in Access2K and capture the full file path. I found an posting below that says this is possible but I cannot...
1
by: Torre Quinn | last post by:
Does anyone have any good sites or resources dealing with adding drag and drop functionality to a set of controls on a form? I'd like to try to get several examples of varied applications of drag...
0
by: ViRi | last post by:
I am currently experimenting a bit with AxMicrosoft.MediaPlayer.Intero­p.AxWindowsMediaPlayer and so far, most has gone well. Currently, i would like to add drag-and-drop functionality to the...
5
by: Markus | last post by:
I tried this: ALTER TABLE Dokumente DROP COLUMN docPrioID but I get this errormessage: DB2 SQL error: SQLCODE: -104, SQLSTATE: 42601, SQLERRMC: COLUMN;ABLE Dokumente DROP;CONSTRAINT
1
by: Darren | last post by:
I'm trying to create a file using drag and drop. I want to be able to select a listview item drag it to the shell and create a file. Each icon in the listview represents a blob in a database. When...
15
by: uwcssa | last post by:
I try to drop a table as: I got: During SQL processing it returned: SQL0478N The object type "TABLE" cannot be dropped because there is an object "sch.SQL070515104729271", of type "FUNCTION",...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.