Solved

Save Confirmation on Selection Change

Posted on 2010-09-13
4
628 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

759 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