jQuery UI's Dialog doesn't work on ASP.NET

2019-03-03 23:54发布

问题:

I have the following test ASPX page:

<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() {

            var dlg = $("#dialog").dialog({
                bgiframe: true,
                autoOpen: false,
                height: 300,
                modal: true,
                buttons: {
                    'Ok': function() {
                        __doPostBack('TreeNew', '');
                        $(this).dialog('close');
                    },
                    Cancel: function() {
                        $(this).dialog('close');
                    }
                },
                close: function() {
                    dlg.parent().appendTo(jQuery('form:first'));
                }
            });
        });
        function ShowDialog() {
            $('#dialog').dialog('open');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="TreeNew" runat="server" Text="New" 
            OnClientClick="ShowDialog();return false;" onclick="TreeNew_Click"/>
        <asp:Label ID="Message" runat="server"></asp:Label>
        <div id="dialog" title="Select content type">
            <p id="validateTips">All form fields are required.</p>
            <asp:RadioButtonList ID="ContentTypeList" runat="server">
                <asp:ListItem Value="1">Texte</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>

I use dlg.parent().appendTo(jQuery('form:first')); on close function to retreive the values from RadioButtonList.

It works well but before the page do the PostBack the div "Dialog" moves below the New button. Why?

回答1:

I think that this is caused because you are calling:

dlg.parent().appendTo(jQuery('form:first'));

at the close callback. This will move the dialog. Why don't you call this immediately after creating the dialog?



回答2:

try chaning $(function() { to $(document).ready(function() {

also check where it fails with some sort of javascript debugger opera got builtin and FireFox got FireBug..