您能幫我嗎? http://www.eggheadcafe.com/articles/javascript_modal_dialog.asp模態對話框 - 返回值並非總是返回
在我的示例代碼中,我用這個對話框三次: 我已經根據本文中的代碼創建一個模式對話框的超鏈接,一個按鈕Button1的與onclick屬性添加和按鈕Button2與OnClientClick事件。 如果我單擊超鏈接,定義對話框的按鈕被單擊的對話框的返回值將轉到文本框。 但是,如果我單擊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
非常感謝, 利
Bobince,謝謝你的回答。不幸的是,有兩種不同的方法不會有幫助:兩種方法都不會返回一個值。所以再說一遍:如果我使用超鏈接來調用模態對話框,它會起作用,如果我使用按鈕 - 不會。更多的想法?至於你的筆記,不使用一個簡單的window.confirm調用的原因是對話框的默認按鈕是OK。我的用戶需要默認按鈕爲取消。這就是爲什麼我試圖處理自定義對話框。期待你的回答。謝謝,列夫 – Lev
它確實返回值;我認爲這只是(調試?)嘗試將它寫入'document.getElementById('modalreturn').value'這是失敗的。您不能逐字使用ASP.NET ID作爲頁面元素ID,因爲ASP.NET會將它們更改爲'ct100-something-modalreturn'和類似的名稱以避免名稱衝突。如果你想這樣做,你必須開始在你的JS中粘貼醜陋的'<%= modalreturn.ClientID%>'ASP。見例如。 http://stackoverflow.com/questions/836121/javascript-and-asp-net-web-user-controls – bobince