【问题标题】:Button in dialog box won't Post back对话框中的按钮不会回发
【发布时间】:2013-05-07 23:43:00
【问题描述】:

我有一个使用 jQuery .dialog() 函数显示的对话框,该对话框包含一个按钮,我想将其发布回另一个页面,但它不起作用。我无法使用PostBackUrl 属性集或OnClick 发布它(这永远不会触发)。但是,当我单击其中一个Calendar 日期时,我认为默认情况下会导致回发,页面回发到我在按钮PostBackUrl 属性中设置的网址!我想使用asp:Calendar,但在选择时禁用默认的回发行为,并且只有在用户选择按钮时才会发布,但显然Calendar 的选定日期在回发时仍然可用。这是.aspx 文件...

<form id="form1" runat="server">
        <asp:Button ID="btnShowDialog" runat="server" Text="Click to Show Dialog" OnClientClick="showDialog(); return false;" />
        <div class="divDialog" style="display: none">
            <table style="width: 100%;">
                <tr>
                    <td>First Name: <asp:TextBox ID="txtFirstName" runat="server" Text=""></asp:TextBox></td>
                    <td>Last Name: <asp:TextBox ID="txtLastName" runat="server" Text=""></asp:TextBox></td>
                </tr>
                <tr>
                    <td>
                        How Old are You?
                        <asp:DropDownList ID="ddlAge" runat="server">
                            <asp:ListItem Value="1">1</asp:ListItem>
                            <asp:ListItem Value="2">2</asp:ListItem>
                            <asp:ListItem Value="3">3</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                    <td>
                        How Many Siblings do You Have?
                        <asp:DropDownList ID="ddlNumberSiblings" runat="server">
                            <asp:ListItem Value="1">1</asp:ListItem>
                            <asp:ListItem Value="2">2</asp:ListItem>
                            <asp:ListItem Value="3">3</asp:ListItem>
                            <asp:ListItem Value="4">4</asp:ListItem>
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Calendar ID="calBirthday" runat="server" Width="200" >
                            <DayStyle BackColor="Yellow"  />
                        </asp:Calendar>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
                    </td>
                </tr>
            </table>
        </div>
    </form>

...和 ​​jQuery 脚本...

   <script>
        function showDialog() {
            $('.divDialog').dialog({ modal: true, show: 'slide', title: 'Please Enter Information Below', width: 500 });
        }
    </script>

...这里是 asp:Button 和 asp:Calendar 中的日期在浏览器中的呈现方式....

<a title="April 28" style="color:Black" href="javascript:__doPostBack('calBirthday','4866')">28</a>

....

<input id="btnSubmit" type="submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSubmit", "", false, "", "Profile.aspx", false, false))" value="Submit" name="btnSubmit">

编辑:按钮提交不起作用,无论我是否包含 asp:Calendar 控件。但是日历控件选择提交出于某种原因有效。

【问题讨论】:

    标签: javascript jquery asp.net post webforms


    【解决方案1】:

    这种对话框的问题在于,它们会将您作为内容提供给它们的内容移动到表单之外的某个位置。

    举个例子:http://jsfiddle.net/Qej8S/1/

    我已将内容放在keeper

    <div class="keeper">
        <div class="divDialog" style="display: none">
        The content of the dialog        
        </div>
    </div>
    

    但是如果运行它,您会看到(以太颜色,以太与浏览器的 dom 实用程序)对话框正在将内容移到 keeper 之外,在您的情况下,将其移到 form 之外为什么不火起来。

    对于解决方案,我想出了一些技巧。创建对话框后,将其移回窗体。这是一个例子。我在表单中放置了一个具有特定 id 的 div:

    &lt;div id="MoveItHere"&gt;&lt;/div&gt;

    我在创建对话框后立即将移动设置为:

    $('.divDialog').dialog({modal: true, show: 'slide', title: 'Please Enter Information Below', width: 200, 
    create: function( event, ui ) 
     {
        // this is the trick, I move it again inside the form, on a specific place.
        $("#MoveItHere").html($(this).parent());
     }
    });
    

    这里是在线测试,如果你看到 dom 就可以了:http://jsfiddle.net/Qej8S/2/

    【讨论】:

    • 啊,我知道你是对的。我想我可以像在 javascript 函数中处理 div.divDialog 一样处理整个表单,对吧?
    • @MassStrike 我知道这个问题,但我暂时没有建议。我正在寻找...
    • @MassStrike 我已经用解决方案更新了我的答案,请查看。可能需要一些调整,如果您打开多次对话框,如果 div 停留在那里,以及其他一些,但它的工作原理。
    • @MassStrike 另请参阅此参数api.jqueryui.com/dialog/#option-appendTo 它似乎可以按照我的尝试进行,但我对其进行了测试并且对我不起作用。也许对你有用。
    • 好的,知道了。它像这样对我有用... function showDialog() { $('.divDialog').dialog({ modal: true, show: 'slide', title: '请在下面输入信息', width: 500, focus: function (event, ui) { $('.divInnerForm').append($(this)); } }); }
    猜你喜欢
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2010-10-19
    • 1970-01-01
    • 2018-11-21
    • 2010-11-09
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多