Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Save Confirmation on Selection Change

Posted on 2010-09-13
4
Medium Priority
?
646 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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
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 …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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