Scott on Writing

Musings on technical writing...

Confirming Before Postback

There are a number of articles out there about building a DataGrid with a ButtonColumn of Delete buttons, and using a bit of client-side JavaScript in the Button's client-side onclick event handler in order to present the user with a confirm popup asking them if they're sure they really want to delete the record.  For example, such an article can be found in An Extensive Examination of the DataGrid Web Control: Part 8.  Essentially you add the onclick event handler to the Button's Attributes collection:

button.Attributes[“onclick“] = “javascript:return confirm(...);“;

I was working on a project recently where a client wanted similar functionality, but with a bit of a twist.  He had a DropDownList populated with some “parent” values, with the parent's details displayed in the Web page.  Changing the DropDownList item would cause the page to be posted back and the page loaded with the newly selected DropDownList's details records.  Simple enough to implement: just add a DropDownList with the AutoPostback property set to True, and write an event handler for the SelectedIndexChanged event.

However, under certain conditions, when the user changed the DropDownList item, this client wanted the user to be prompted with a client-side confirm box, asking them if they were sure they wanted to leave the current details information.  (Imagine, for instance, that the details for a particular master record had some “critical” tasks that needed to be done, and if these had not been checked off, a warning would need to be displayed saying something like, “Are you sure you want to leave, you still have critical tasks left unchecked.”)

To accomplish this, I needed to add an onchange client-side event handler for the DropDownList.  However, the DropDownList already emits some client-side script in the onchange event - namely the code to initiate a postback.  That is, the DropDownList, with AutoPostback set to True, renders like:

<select onchange=”__doPostBack(...)”>
   ...
</select>

Adding an onchange event to the DropDownList's Attributes collection prepends whatever you add to the __doPostBack(...) script emitted.  So, we want to add script that calls __doPostBack(...) only if the user hits OK in the confirm popup.  The following will accomplish this:

ddl.Attributes[”onchange”] = “if (confirm(...)) “;

With this client-side script, the DropDownList will be rendered as:

<select onchange=”if (confirm(...)) __doPostBack(...)”>
   ...
</select>

And that's what we want!  Now, if the user changes the DropDownList value, it will popup a client-side confirm box, saying, “Are you sure you want to change the master record?” (or whatever).  If the user clicks OK, it will postback and update the detail view.  If they click cancel, the postback will not occur, and the user will remain on the same page.

This approach, though, is not complete, I'm afraid.  If the user clicks cancel it's true that the page won't be posted back, but the DropDownList will display the value of the selected list item.  That is, if the user is viewing the master record X, and then change to Y, they'll see that confirm box.  If they click cancel, a postback won't occur, but the DropDownList will show Y (not X).

One approach to fix this is to add a bit of client-side code to the page that records the DropDownList's selected index on the body's onload event.  Then, a client-side function can be created that resets the DropDownList's selected index back to the original one.  This function would be called if the user clicked cancel.  To get everything to work, the onchange script needs to be changed to:

ddl.Attributes[”onchange”] = “if (!confirm(...)) resetIndexes(); else “;

With this client-side script, the DropDownList will be rendered as:

<select onchange=”if (!confirm(...)) resetIndexes(); else __doPostBack(...)”>
   ...
</select>

So if the user clicks Cancel on the confirm, the client-side resetIndexes() function will be called, which will reset the DropDownList index.  Otherwise, if they clicked OK, the postback will occur.  The final piece of the puzzle is the code for the resetIndexes() function and the function that records the original selected index values in the onload event.  Assuming your DropDownList's client ID is foo, this could be accomplished with the following client-side code:

var fooIndex;

function saveIndexes()
{
   fooIndex = document.frmName.foo.selectedIndex;
}

function resetIndexes()
{
   document.frmName.foo.selectedIndex = fooIndex;
}
 

This assumes that you have named your Web Form frmName.  (<form runat=”server” id=”frmName”>).  Also, you'll need to call saveIndexes() in the body onload event - <body onload=”saveIndexes();”>.

Hope this helps someone...  :-)

posted on Friday, June 18, 2004 2:35 PM

Feedback

# re: Confirming Before Postback 6/22/2004 4:18 PM Scott Hanselman

Are you using Page.GetPostBackEventReference?

http://www.hanselman.com/blog/PermaLink.aspx?guid=9059256e-fa67-4dc7-9144-8ff35d7f36cb

# re: Confirming Before Postback 9/2/2004 11:23 AM Prashu

Wht should go in the else clause??

if (!confirm(...)) resetIndexes(); else ??



# re: Confirming Before Postback 9/2/2004 12:16 PM Scott Mitchell

Prashu, you don't have to put anything there. It gets automatically filled to __doPostBack();

# re: Confirming Before Postback 9/7/2004 1:32 PM Jerry

I was confronting a similar problem and came up with this solution/cluge earlier today -- then I went searching to see if there was a better way to do this, 'cause this seems like a total hack. So, let's discuss this a bit more...

Does anyone consider this approach good practice? How would you handle TWO if clause statements, just add curly braces? Is there a hotfix that allows you to handle this by placing code in your form's "OnPostBack" event?

http://www.shinydonkey.com/thread.aspx?Thread=423

# re: Confirming Before Postback 1/18/2005 5:21 AM Bruce

maybe this could help.

http://www.codingforums.com/archive/index.php/t-29410

# re: Confirming Before Postback 3/18/2005 9:25 AM Nirbho

Top post! Really useful. Thanks for sharing.

# re: Confirming Before Postback 4/5/2005 8:55 AM Brett

Thanks for this.. this really helped me out. I had a query getting launched on the server-side dropdownlist SelectedIndexChanged event and the query was taking a long time so I wanted to show a busy DIV label right when the ddl was changed. Works like a champ except the animated gif I am using won't animate. Not a big deal.

# re: Confirming Before Postback 4/25/2005 1:24 PM N

thank you scott

# re: Confirming Before Postback 6/9/2005 1:37 AM NoName

Thanks ALOT!!!! Ive been searching like forever for it!!

# re: Confirming Before Postback 8/14/2005 5:55 AM Amruta

Thanks scott :-)

# re: Confirming Before Postback 10/31/2005 9:21 AM VG

Thanks Scott, helped me out a lot

# re: Confirming Before Postback 11/9/2005 1:08 AM murali

Really This site looks very worst. Please modify it

# re: Confirming Before Postback 11/21/2005 7:42 PM saimatkong

hey, i still can't figure out how to activate popup from dropdownlist postback.

can show me the full code of doing that ?
thanks

# re: Confirming Before Postback 11/21/2005 8:14 PM saimatkong

in asp.net there's no "onchange" ??

# BDSM Domination 1/5/2006 11:30 PM I was wondering what everyone thinks about Dominat

I was wondering what everyone thinks about Domination! Personally I think occasionally it&#039;s rather interesting to practice this in the bedroom with my husband. The first time we did it I got to see a side of him that you wouldn&#039;t know was there. <a href=&#039;http://www.bdsm-domination.be&#039;>http://www.bdsm-domination.be</a> - LOL So seeing that side of him, I thought was highly erotic! Anyone else enjoy a big of BDSM? If so, how far have you ever taken it? Do you use Code words?

# re: Confirming Before Postback 10/4/2006 5:07 PM GC

Thanks Scott! This is really useful!

# re: Confirming Before Postback 10/26/2006 1:13 PM Nathan Prather

Scott,

You rule! I use your code constantly. I'm currently using the methods described in the 3 tier data access www.asp.net/learn

Keep up the good work! My career depends on it :-)

Thanks,
Nathan

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

Add To Your Reader

My Links

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<May 2008>
SMTWTFS
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

Comment Stats

DayTotal% of Total
Sunday 1866.8%
Monday 37913.9%
Tuesday 45316.7%
Wednesday 50418.5%
Thursday 53519.7%
Friday 49418.2%
Saturday 1666.1%
Total 2717100.0%

Hour1Total% of Total
12:00 AM 652.4%
1:00 AM 682.5%
2:00 AM 622.3%
3:00 AM 742.7%
4:00 AM 572.1%
5:00 AM 1033.8%
6:00 AM 1084.0%
7:00 AM 1585.8%
8:00 AM 1716.3%
9:00 AM 1475.4%
10:00 AM 1716.3%
11:00 AM 1816.7%
12:00 PM 1886.9%
1:00 PM 1696.2%
2:00 PM 1605.9%
3:00 PM 1324.9%
4:00 PM 1073.9%
5:00 PM 923.4%
6:00 PM 913.3%
7:00 PM 963.5%
8:00 PM 833.1%
9:00 PM 782.9%
10:00 PM 792.9%
11:00 PM 772.8%
Total 2717100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.54144
Monday 5.22339
Tuesday 4.28419
Wednesday 7.67637
Thursday 6.90607
Friday 5.48411
Saturday 5.33160
Total 5.842717

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.35107
9:00 AM 6.32278
10:00 AM 6.47246
11:00 AM 4.41181
12:00 PM 6.88330
1:00 PM 3.00111
2:00 PM 5.41222
3:00 PM 8.64285
4:00 PM 4.0589
5:00 PM 5.92154
6:00 PM 4.52113
7:00 PM 9.67174
8:00 PM 9.80147
9:00 PM 5.05111
10:00 PM 5.4265
11:00 PM 4.5732
Total 5.842717

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles