【发布时间】:2009-11-17 20:43:45
【问题描述】:
你能帮帮我吗? 我根据本文中显示的代码创建了一个模态对话框: http://www.eggheadcafe.com/articles/javascript_modal_dialog.asp
在我的示例代码中,我三次使用此对话框:对于超链接,添加了 onclick 属性的按钮 Button1 和带有 OnClientClick 事件的按钮 Button2。 如果单击超链接,则定义单击的对话框按钮的对话框的返回值将转到文本框。 但是,如果我单击 Button1 或 Button2,则无法获得返回值,即确定单击了对话框的哪个按钮。
能否请您帮我找到获取对话框返回值的正确方法? 我对添加了 onclick 属性的 Button1 的情况特别感兴趣。
下面是我的页面和代码隐藏测试代码。
== 页面 ==
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ModalDialogTest1.aspx.vb" Inherits="ModalDialogTest1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script language="javascript">
var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;
ModalDialog.value = '';
ModalDialog.eventhandler = '';
function ModalDialogMaintainFocus()
{
try
{
if (ModalDialogWindow.closed)
{
window.clearInterval(ModalDialogInterval);
eval(ModalDialog.eventhandler);
return;
}
ModalDialogWindow.focus();
}
catch (everything) { }
}
function ModalDialogRemoveWatch()
{
ModalDialog.value = '';
ModalDialog.eventhandler = '';
}
function ModalDialogShow(Title,BodyText,Buttons,EventHandler)
{
ModalDialogRemoveWatch();
ModalDialog.eventhandler = EventHandler;
var args='width=350,height=125,left=325,top=300,toolbar=0,';
args+='location=0,status=0,menubar=0,scrollbars=1,resizable=0';
ModalDialogWindow=window.open("","",args);
ModalDialogWindow.document.open();
ModalDialogWindow.document.write('<html>');
ModalDialogWindow.document.write('<head>');
ModalDialogWindow.document.write('<title>' + Title + '</title>');
ModalDialogWindow.document.write('<script' + ' language=JavaScript>');
ModalDialogWindow.document.write('function CloseForm(Response) ');
ModalDialogWindow.document.write('{ ');
ModalDialogWindow.document.write(' window.opener.ModalDialog.value = Response; ');
ModalDialogWindow.document.write(' window.close(); ');
ModalDialogWindow.document.write('} ');
ModalDialogWindow.document.write('</script' + '>');
ModalDialogWindow.document.write('</head>');
ModalDialogWindow.document.write('<body onblur="window.focus();">');
ModalDialogWindow.document.write('<table border=0 width="95%" align=center cellspacing=0 cellpadding=2>');
ModalDialogWindow.document.write('<tr><td align=left>' + BodyText + '</td></tr>');
ModalDialogWindow.document.write('<tr><td align=left><br></td></tr>');
ModalDialogWindow.document.write('<tr><td align=center>' + Buttons + '</td></tr>');
ModalDialogWindow.document.write('</body>');
ModalDialogWindow.document.write('</html>');
ModalDialogWindow.document.close();
ModalDialogWindow.focus();
ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",5);
}
</script>
<script language=JavaScript>
function OKCancel_1(BodyText, EventHandler) {
var Buttons = '';
Buttons = '<input type="submit" value="Cancel" class="butt" style="width:100px;" onclick=javascript:CloseForm("Cancel");> ';
Buttons += '<input type="submit" value="OK" class="butt" style="width:100px;" onclick=javascript:CloseForm("OK");> ';
ModalDialogShow("Dialog", BodyText, Buttons, EventHandler);
}
function NoYes(BodyText, EventHandler) {
var Buttons = '';
Buttons = '<input type="submit" value="No" class="butt" style="width:100px;" onclick=javascript:CloseForm("No");> ';
Buttons += '<input type="submit" value="Yes" class="butt" style="width:100px;" onclick=javascript:CloseForm("Yes");> ';
ModalDialogShow("Dialog", BodyText, Buttons, EventHandler);
}
function OKCancelReturnMethod() {
document.getElementById('OKCancelReturn').value = ModalDialog.value;
ModalDialogRemoveWatch();
}
function NoYesReturnMethod() {
document.getElementById('modalreturn').value = ModalDialog.value;
ModalDialogRemoveWatch();
}
</script>
<body>
<form id="form2" runat="server">
<div>
<asp:HyperLink ID="HyperLink3" runat="server"
NavigateUrl="javascript:OKCancel_1('OKCancel test','OKCancelReturnMethod()');">OK/Cancel_1
</asp:HyperLink>
<br /><br />
<asp:Label ID="Label1" runat="server" Text="OKCancelReturn:"></asp:Label>
<asp:TextBox ID="OKCancelReturn" runat="server"></asp:TextBox>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Button -> NoYes onclick" >
</asp:Button>
<br /><br />
<asp:Button ID="Button2" runat="server" Text="Button -> NoYes OnClientClick"
OnClientClick="javascript:NoYes('NoYes test','NoYesReturnMethod()');">
</asp:Button>
<br /><br />
<asp:Label ID="Label2" runat="server" Text="modalreturn:"></asp:Label>
<asp:TextBox ID="modalreturn" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
== 代码隐藏===
Partial Class ModalDialogTest1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim message As String
message = "Test Message: Do you want to delete?"
Button1.Attributes("onclick") = GetConfirmationScript(message)
End Sub
Private Function GetConfirmationScript(ByVal message As String) As String
Dim output As String
output = "javascript:NoYes('" & message & "','NoYesReturnMethod()');"
Return output
End Function
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
OKCancelReturn.Text = modalreturn.Text
End Sub
End Class
非常感谢, 列弗
【问题讨论】:
标签: asp.net