MsCorEE

JeffGonzalez : IScalable

ModalPopupExtender experiences

I ran into a problem today while working with the ModalPopupExtender.  It was a self imposed problem, but it through me for a loop just the same. I made a confirmation modal window.  I have a need for a user to make a decision when they save something to the database. 

So what I did was create a panel like this:

<asp:Panel id="SamplePanel" CssClass="modal-popup" style='display:none;' runat='server'>
<table>
<tr>
<td class="modal-content">
<p>
This is a message detailing a warning.
</p>
<p class="modal-warning">
Do you want to proceed with this action?
</p>
<asp:button id="YesButton" runat="server" text="Yes" cssclass="modal-button"/>
<asp:button id="NoButton" runat="server" text="No" cssclass="modal-button"/>
</td>
</tr>
</table>
</asp:Panel>

This allows the user to choose yes or no, and it sets a hidden form field with this value.  I set up my ModalPopupExtender like this:

<ajax:ModalPopupExtender 
id="ModalPopupExtender"
runat="server"
dropshadow="true"
targetcontrolid="SaveButton"
popupcontrolid="SamplePanel"
backgroundcssclass="modal-background"
okcontrolid="YesButton"
onokscript="onConfirm()"
cancelcontrolid="NoButton"
oncancelscript="onCancel()"/>
 
With this I was able to popup my modal window.  I had to craft some javascript for the ok/cancel stuff still.  I did that by registering a script block from the codebehind (Thanks Shannon!).  Here it is:

private void BuildModalScript()
{
StringBuilder yesScript = new StringBuilder();
StringBuilder noScript = new StringBuilder();

yesScript.AppendFormat("function onConfirm(){0}", Environment.NewLine);
yesScript.Append("{").Append(Environment.NewLine);
yesScript.AppendFormat("var hidden = $get(\"{0}\");{1}", HiddenField.ClientID, Environment.NewLine);
yesScript.AppendFormat("hidden.value = \"true\";{0}", Environment.NewLine);
yesScript.Append(Page.ClientScript.GetPostBackEventReference(SaveButton, String.Empty));
yesScript.Append("}").Append(Environment.NewLine);

noScript.AppendFormat("function onCancel(){0}", Environment.NewLine);
noScript.Append("{").Append(Environment.NewLine);
noScript.AppendFormat("var hidden = $get(\"{0}\");{1}", HiddenField.ClientID, Environment.NewLine);
noScript.AppendFormat("hidden.value = \"false\";{0}", Environment.NewLine);
noScript.Append(Page.ClientScript.GetPostBackEventReference(SaveButton, String.Empty)).Append(Environment.NewLine);
noScript.Append("}").Append(Environment.NewLine);

if (!Page.ClientScript.IsClientScriptBlockRegistered("onConfirm"))
Page.ClientScript.RegisterClientScriptBlock(typeof(_Default), "onConfirm", yesScript.ToString(), true);

if (!Page.ClientScript.IsClientScriptBlockRegistered("onCancel"))
Page.ClientScript.RegisterClientScriptBlock(typeof(_Default), "onCancel", noScript.ToString(), true);
}

When I first started this little project, I was used to faking postbacks using the script like this:  __doPostBack(Control.ClientId);   Oddly that works if you set the enableeventvalidation property to true.  However, it doesn't work if your page is part of a masterpage.  The lead architect, Brian, at my new job and I were banging our heads against the wall trying to figure it out.  He knew there was something in .net that handled this but he couldn't think of the name.  I remembered seeing something like postbackreference during my travels through the documentation.  It turned out to be the correct answer.  I guess you could call it a team effort =P  It was really weird the way the problem manifested itself, because I had two different projects, it worked in one and not the other.  Once I made them exactly the same, we could see there was definitely an issue with master pages.  So chalk that one up to experience.

In any case, I now have a nifty modal confirmation dialog.  Here is the project for vs2005 if you want to take a look.


Comments

interscape said:

I've worked with ModalPopupExtenders before... why was all that stringbuilder stuff necessary? Seems to just overcomplicate the code...

# March 3, 2007 1:37 AM

likwid said:

I needed to write to a hidden form field from the modal window.  The requirement for the UI is that if the user saves this particular form (it is a crud data entry page), they need to be warned that the change they plan to make has some consequences.  

The choice is either yes or no, which correlate to the onOk and onCancel client side functions.

Since I need to write to the hidden form field, I needed to output the script so I could get a reference to the form field from the client side.

Unless you know another way to do it...

# March 3, 2007 3:55 AM

rbuckton said:

Theres a simpler way to achieve what you're looking for.  When associated to the ModalPopupExtender the "ok" and "cancel" buttons' purpose is to allow the popup to collapse without a postback.  Since a Postback effectively collapses the dialog you can omit the OkButtonID attribute and let the asp:Button perform its own postback.

If you need to perform some client scripting prior to the postback, just use the OnClientClick property of the asp:Button to perform the actions (such as setting the value of a hidden field).

# March 3, 2007 7:46 PM

likwid said:

Thanks Ron, you are right that is a much simpler way.  I was able to trim a lot of code out and it actually worked better this way too.

Thanks for the tip.

# March 7, 2007 9:06 PM

Abbott said:

I tried to download the sample code you created, but the link is missing - thanks for the insight. It also fixed another problem I was having.

# January 9, 2008 7:39 AM

cjDuva said:

Thank you, rbuckton.

What is plenty weird is that I had one CRUD form where the server-side code WOULD run when the okControlID was clicked, and one where it wouldn't.

It really does appear that MS needs to put tha AJAX gang and the MasterPages/FormView gang in the same room for a little while.

# April 9, 2008 8:41 AM

Steve said:

So, what's the exact code fix for getting ModalPopupExtender to work correctly with Master Pages?  I've got it working correctly except the buttons on my Master Page are not disabled when the Popup is fired.  Please help as I am new to AJAX.  

Thanks in advance.

# February 12, 2009 6:07 PM

Masada said:

Hi all. Life is a great big canvas; throw all the paint on it you can. Help me! Need information about: Follow these baby bedding and crib bumper safety guidelines to make sure your baby sleeps snug and safe.. I found only this - <a href="http://turbo-tax.biz">turbo tax</a>. Cocalo baby bedding and accessories baby bedding cocalo is a creative company cocalo a la mode baby nursery bedding and accessories. Finest linens and things offers baby crib bedding at discount and luxury prices. Thank ;-) Masada from Kenya.

# May 24, 2009 5:45 PM

ModalPopupExtender experiences « Evil Science said:

Pingback from  ModalPopupExtender experiences &laquo;  Evil Science

# June 28, 2009 6:27 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)