简体   繁体   English

模拟jQuery对话框中的另一个按钮回发

[英]Simulate a another button postback inside a jQuery dialog

I have the following ASPX page: 我有以下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>

When the user click on TreeNew button appears the modal dialog, then he/she choose an option an click Ok button to do a postback. 当用户单击TreeNew按钮时,将出现模式对话框,然后他/她选择一个选项,然后单击“确定”按钮进行回发。

I need that the server side execute TreeNew_Click method: How can I do that? 我需要服务器端执行TreeNew_Click方法:如何做到这一点?

If I use __doPostBack('TreeNew', '') it throws me the following error: "Object expected". 如果我使用__doPostBack('TreeNew',''),则会抛出以下错误:“预期对象”。

UPDATE: 更新:
I found the origin for the error: the function __doPostBack is not defined. 我发现了错误的根源:未定义函数__doPostBack I'm not going to delete the question because I think Chris Clark's answer is so interesting. 我不会删除该问题,因为我认为Chris Clark的答案非常有趣。

As a rule, if you find yourself ever typing the text "__doPostBack(...", you should re-evaluate your approach. 通常,如果您曾经键入文本“ __doPostBack(...)”,则应重新评估您的方法。

In this case, you should just put a server-side asp.net button inside the div that you are turning into the dialog and use that instead of the generates jQuery button. 在这种情况下,您应该只将服务器端的asp.net按钮放在要变成对话框的div内,并使用它代替生成jQuery按钮。 That way the postback code will get wired up for you. 这样,回发代码将为您连接起来。 There is one caveat however - when jQuery turns your div (I'm going to assuming it's a div) into a dialog, it rips the div out of the form element . 但是有一个警告:当jQuery将您的div(我假设它是div)变成对话框时,它将div从form元素中剔除 That means you have to attach it BACK to the form BEFORE the postback occurs. 这意味着您必须在回发发生之前将其附加回表单。 You can do that in the close function of the dialog. 您可以在对话框的关闭功能中执行此操作。 The postback will then occur properly. 回发将正确进行。

If you really want to use the generated jQuery OK button, you have a couple of options. 如果您确实要使用生成的jQuery OK按钮,则有两种选择。 First, you can slap a server-side asp.net button on the page and hide it, then call the asp.net button's click event from the OK button's code. 首先,您可以在页面上拍一个服务器端的asp.net按钮并将其隐藏,然后从“确定”按钮的代码中调用asp.net按钮的click事件。 Second, you can emit a javascript function form the server using Page.ClientScript.GetPostBackEventReference that will contain the __doPostBack code that you were trying to hand-write above. 其次,您可以使用Page.ClientScript.GetPostBackEventReference从服务器发出一个javascript函数,该函数将包含您试图在上面手写的__doPostBack代码。 Then call that emitted function from the OK button's JS code. 然后从“确定”按钮的JS代码中调用该发出的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM