【问题标题】:Modal Dialog Box - return value not always returned模态对话框 - 返回值并不总是返回
【发布时间】: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");>&nbsp;&nbsp;';
        Buttons += '<input type="submit" value="OK" class="butt" style="width:100px;" onclick=javascript:CloseForm("OK");>&nbsp;&nbsp;';
        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");>&nbsp;&nbsp;';
        Buttons += '<input type="submit" value="Yes" class="butt" style="width:100px;" onclick=javascript:CloseForm("Yes");>&nbsp;&nbsp;';
        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


    【解决方案1】:

    您需要有两个 NoYesReturnMethods,并在 Button1 和 Button2 的 onclick 中传递一个不同的方法。

    OnClientClick="NoYes('NoYes test','NoYesReturnMethod1()')
    

    (对于 button2 -> NoYesReturnMethod2 也是如此。)

    但是,这可能不是您想听到的,但是您基于此的代码非常糟糕。它比它需要的要复杂得多,并且滥用evaljavascript: URL、未转义的 HTML 字符串转义、无铬弹出窗口和丢弃异常......这就像一个最糟糕的做法的抓包。

    如果您的所有案例都只是带有是/否按钮的消息,那么您最好还是坚持一个琐碎的window.confirm 电话。如果您可能需要包含更多涉及的对话形式,请尝试使用许多其他页面内对话框脚本/插件之一。

    【讨论】:

    • 博宾斯,感谢您的回答。不幸的是,拥有两种不同的方法并没有帮助:这两种方法都没有返回值。再说一遍:如果我使用超链接来调用模态对话框,它会起作用,如果我使用按钮 - 不会。更多想法?至于您的注意,不使用微不足道的 window.confirm 调用的原因是对话框的默认按钮是确定的。我的用户需要默认按钮为取消。这就是我尝试处理自定义对话框的原因。期待您的回答。谢谢,列夫
    • 它确实返回了值;我认为这只是(调试?)尝试将其写入document.getElementById('modalreturn').value 失败。您不能将 ASP.NET ID 逐字用作页面元素 ID,因为 ASP.NET 将它们更改为 ct100-something-modalreturn 和类似内容以避免名称冲突。如果你想这样做,你必须开始在你的 JS 中粘贴丑陋的&lt;%= modalreturn.ClientID %&gt; ASP。参见例如。 stackoverflow.com/questions/836121/…
    猜你喜欢
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 2020-06-27
    • 2021-09-22
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多