Solved

Save Confirmation on Selection Change

Posted on 2010-09-13
4
587 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
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:karthitron
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now