Solved

Save Confirmation on Selection Change

Posted on 2010-09-13
4
622 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX ModalPopupExtender will Not Hide in Asp.net 1 34
Finding the IIS version 5 37
Change to file doesn't show up 16 74
asp.net mvc 2 33
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

763 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