Solved

Save Confirmation on Selection Change

Posted on 2010-09-13
4
635 Views
Last Modified: 2012-05-10
I'm working on adding a 'do you want to save" dialog to a web page.  For example, if I navigate away from the page and you've made changes it'll ask if you want to save changes.

On the screen I have a drop down list to select different records you can edit.  If you've made a change to the record I want it to ask if you want to save changes when you change the drop down list selection.  I thought I could just add some javascript to the onChange event.  However, ASP.NET already added a onChange event handler to do the auto postback.  Does anyone know how I can do this?


Thanks,
0
Comment
Question by:ScottMacMaster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:karthitron
ID: 33662477
Eventhough, ASP.Net wiil use the 'onchange' event, you can call your javascript method there. First your piece of code will execute and then only ASP.Net code for auto-postback will execute. So you can use the onchange event to process your requriement.
Following is sample code for dropdownlist control:
onchange="alert(' Validation function comes here, Before Postback');"
Before and after onchange event generated by ASP.Net:
Before:
 onchange="setTimeout('__doPostBack(\'ctl00$cphBody$ddlTest\',\'\')', 0)"
After:
 onchange="alert(' Validation function comes here, Before Postback');setTimeout('__doPostBack(\'ctl00$cphBody$ddlTest\',\'\')', 0)"
0
 

Author Comment

by:ScottMacMaster
ID: 33662550
It did cross my mind to do something like this.  However, I'm concerned that ASP.NET will overwrite what I put in there with it's autopostback code.  Can I safely add in my code during the Load() event or do I need to do it during a later point in the page processing?


Thanks,
0
 
LVL 5

Accepted Solution

by:
karthitron earned 500 total points
ID: 33662649
No need to do any extra stuff, simply call the onchange event for the asp:dropdownlist control. It will fire our javascript stuff first and then fires their autopostback stuff. i already displayed the view source before and after we adding the onchange event, here I'm again pasting the same:
Ex:
<asp:DropDownList ID="ddlTest" runat="server" onchange="alert(1);" AutoPostBack="true" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged" > 
<asp:ListItem Text="dsf"></asp:ListItem>
<asp:ListItem Text="rtyr"></asp:ListItem>
</asp:DropDownList>

Following is sample code for dropdownlist control:
onchange="alert(1);"
Before and after onchange event generated by ASP.Net:
Before:
onchange="setTimeout('__doPostBack(\'ctl00$cphBody$ddlTest\',\'\')', 0)"
After:
onchange="alert(1);setTimeout('__doPostBack(\'ctl00$cphBody$ddlTest\',\'\')', 0)"  
 
Instead of writing alert, you can call your custom javascript method to do validation and return the true or false. Based on return value, you can allow to execute following code generated for auto-posteback event. Following is the example:
function IsValidationPass()
{
if(NO CHANGE FOUND)
{
return true;
}
else
{
return false;
}
}
onchange="if(!IsValidationPass()){return false;}"  
 
So ASP.Net will generate code like following:
onchange="if(!IsValidationPass()){return false;} setTimeout('__doPostBack(\'ctl00$cphBody$ddlTest\',\'\')', 0)"  
0
 

Author Comment

by:ScottMacMaster
ID: 33662901
Ok, thanks.  That worked.  I was just overly concerned ASP.NET would overwrite my onChange code with it's auto postback code.  Also, I was thinking I needed to return false to somehow cancel the postback like I can do with OnClientClick on buttons.  However, this'll be nicer then cancelling the postback and then having the user click a save button.  I'll just have the javascript set a hidden control to some value  to tell the postback handler to save changes before it loads the selected record.

Thanks,
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question