Modal dialogs in .NET

Skip Navigation LinksIrocon > Blog > 2005 > November, 2005 > November 05, 2005 > Modal dialogs in .NET

Modal dialogs in .NET

A comment by Szymon Kobalczyk on this thread explains how to show modal dialogs in asp.net.


I did this just last week. I had a little problem though: when you put a
WebForm in a modal all postback or navigation will go to another, new window
instead of the dialog. You can overcome this by puting the dialog inside of
a FRAME. So now I have a simple html page that I open in the dialog and then
it loads the actual content. Here is the code of this file:


<meta name="GENERATOR" content="Microsoft Visual Studio 7.0">
<script id="clientEventHandlersJS" language="javascript">
<!-- function window_onload() {
if (window.dialogArguments != null) {
document.all.targetFrame.src=window.dialogArguments; } } //-->
</script>

<iframe id="targetFrame" height="100%" width="100%"></iframe>


I've made a simple client side function to handle the dialog:

function showDialog(url, width, height) {
var sFeatures;
sFeatures="dialogWidth: "+width+"px; ";
sFeatures+="dialogHeight: "+height+"px; ";
sFeatures+="help: no; ";
sFeatures+="resizable: no; ";
sFeatures+="scroll: no; ";
sFeatures+="status: no; ";
sFeatures+="unadorned: no; ";

var result;
result = window.showModalDialog("modalDialog.htm",url, sFeatures)
return result;
}


You pass it the name of the content page and size of the window. It will
return the result.

Then to open the dialog from hyperlink you simply use:

<asp:hyperlink id="HyperLink1" runat="server" navigateurl="javascript:showDialog('newDialog.aspx',340,240)">Open
Dialog</asp:hyperlink>



Last on the content you have to attach code to close the window. This one I
emit on the server side after the button is clicked but you can do it client
side if you want:

private void OkBtn_Click(object sender, System.EventArgs e)
{
Response.Write("<script>window.returnValue = 'ok';
window.close();</script>");
}


I hope this helps.
Regards,

Szymon Kobalczyk.


However, I remember coming across a post somewhere that fixes the postback problem. I'll find it.
Last Updated:Thursday, September 02, 2010By:alferoSource#

Would you like to comment?

Sign in with your OpenID. If you do not know what OpenID is, find out.