【问题标题】:Simulate a another button postback inside a jQuery dialog在 jQuery 对话框中模拟另一个按钮回发
【发布时间】:2010-02-23 20:13:37
【问题描述】:

我有以下 ASPX 页面:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
    <script src="js/jquery-ui-1.6.custom.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function() {

            $("#dialog").dialog({
                bgiframe: true,
                autoOpen: false,
                height: 300,
                modal: true,
                buttons: {
                    'Ok': function() {
                        $(this).dialog('close');
                        __doPostBack('TreeNew', '');
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                    ;
                }
            });
        });
        function ShowDialog() {
            $('#dialog').dialog('open');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="TreeNew" runat="server" Text="Nuevo" OnClientClick="ShowDialog(); return false;"/>
        <asp:Label ID="Message" runat="server"></asp:Label>
        <div id="dialog" title="Create new user">
            <p id="validateTips">All form fields are required.</p>
            <asp:RadioButtonList ID="ContentTypeList" runat="server">
                <asp:ListItem Value="1">Text</asp:ListItem>
                <asp:ListItem Value="2">Image</asp:ListItem>
                <asp:ListItem Value="3">Audio</asp:ListItem>
                <asp:ListItem Value="4">Video</asp:ListItem>
        </asp:RadioButtonList>
        </div>
    </div>
    </form>
</body>
</html>

当用户点击 TreeNew 按钮时出现模态对话框,然后他/她选择一个选项并点击 Ok 按钮进行回发。

我需要服务器端执行 TreeNew_Click 方法:我该怎么做?

如果我使用 __doPostBack('TreeNew', '') 它会抛出以下错误:“Object expected”。

更新:
我找到了错误的根源:未定义函数 __doPostBack。我不会删除这个问题,因为我认为 Chris Clark 的回答很有趣。

【问题讨论】:

    标签: c# asp.net jquery-ui webforms modal-dialog


    【解决方案1】:

    通常,如果您发现自己曾经输入过文本“__doPostBack(...”,您应该重新评估您的方法。

    在这种情况下,您应该只将一个服务器端 asp.net 按钮放在您要转换为对话框的 div 中,并使用它而不是生成 jQuery 按钮。这样,回发代码将为您连接起来。但是有一个警告 - 当 jQuery 将您的 div(我将假设它是一个 div)变成一个对话框时,它将 div 从表单元素中剥离。这意味着您必须在回发发生之前将其附加回表单。您可以在对话框的关闭功能中执行此操作。然后回发将正确进行。

    如果你真的想使用生成的 jQuery OK 按钮,你有几个选择。首先,您可以在页面上打一个服务器端的 asp.net 按钮并将其隐藏,然后从 OK 按钮的代码中调用 asp.net 按钮的单击事件。其次,您可以使用 Page.ClientScript.GetPostBackEventReference 从服务器发出一个 javascript 函数,该函数将包含您在上面尝试手写的 __doPostBack 代码。然后从 OK 按钮的 JS 代码中调用该发出的函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多