简体   繁体   English

如何在具有母版页的页面中打开jQuery UI对话框?

[英]How to open jQuery UI dialog in page with master page?

I want to open a jQuery UI Dialog from a asp.net page with master page. 我想从带有母版页的asp.net页打开jQuery UI对话框。 In a page within master page the code works fine but with master page doesn't work. 在母版页中的页面中,代码可以正常工作,但在母版页中不起作用。

The code is: 代码是:

<%@ Page Title="" Language="vb" AutoEventWireup="false"   MasterPageFile="~/privado/master/interior.master" CodeBehind="WebForm3.aspx.vb" Inherits="ProyectoDemo.WebForm3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="headInterior" runat="server">
    <link type="text/css" href="Styles/jquery-ui-1.8.16.custom.css"    rel="stylesheet" />  
    <link href="Styles/addhunters.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui- 1.8.16.custom.min.js"></script>
    <style type="text/css">
        /*demo page css*/
        body{ font: 62.5% "Trebuchet MS", sans-serif; margin: 50px;}
        .demoHeaders { margin-top: 2em; }
        #dialog_link {padding: .4em 1em .4em 20px;text-decoration:  none;position: relative;}
        #dialog_link span.ui-icon {margin: 0 5px 0 0;position: absolute;left: .2em;top: 50%;margin-top: -8px;}
        ul#icons {margin: 0; padding: 0;}
        ul#icons li {margin: 2px; position: relative; padding: 4px 0; cursor: pointer; float: left;  list-style: none;}
        ul#icons span.ui-icon {float: left; margin: 0 4px;}
    </style>    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="contenidoInterior" runat="server">
    <script type="text/javascript">
        $(function () {
            // Dialog      
            $('#dialog').dialog({
                autoOpen: false,
                width: 600,
                height: 400,
                buttons: {
                    "Ok": function () {
                        $(this).dialog("close");
                    },
                    "Cancelar": function () {
                        $(this).dialog("close");
                    }
                }
            });
            // Dialog Link
            $('#Boton').click(function () {
                $('#dialog').dialog('open');
                return false;
            });
        });
    </script>

    <div id="uno">
        <h2 class="demoHeaders">Dialog</h2>        
        <asp:TextBox ID="TextBox1" runat="server" Text="prueba" ></asp:TextBox>
        <p><a href="#" id="dialog_link" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-newwin"></span>Open Dialog</a></p>              
        <asp:LinkButton ID="Boton" runat="server">LinkButton</asp:LinkButton>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <!-- ui-dialog -->
        <div id="dialog" title="Dialog Title">
            <p>Dialog text</p>            
        </div>  
    </div>

I think the problem is how to refer to "dialog" within placeholder. 我认为问题是如何在占位符中引用“对话框”。 I tried with several ways but I don't find the solution. 我尝试了几种方法,但找不到解决方案。

Anybody knows how to solve this issue? 有人知道如何解决这个问题吗?

Thank you in advance! 先感谢您!

Javier 哈维尔

There are several things that you need to consider: 您需要考虑以下几件事:

  1. Add following code to a separate common.js and add it's reference to master page: 将以下代码添加到单独的common.js中,并将其引用添加到母版页:

window["common"] = {
    liveDialog: function(btnId) {
         $(btnId).live(click,common.showDialog);
         return false;
    },
    showDialog() : function(){
         $('#dialog').dialog({
                autoOpen: false,
                width: 600,
                height: 400,
                buttons: {
                    "Ok": function () {
                        $(this).dialog("close");
                    },
                    "Cancelar": function () {
                        $(this).dialog("close");
                    }
                }
            });

    }

}
  1. Add following method to master page: 将以下方法添加到母版页:
public void RegisterDialog(clientBtnId)
{
    this.page.ClientScript.RegisterStartupScript(this.Page.GetType(),"__registerDialg","common.liveDialog('"+clientBtnId+"');",true);
}
  1. From your content page, cast this.MasterPage to actual class name of your master page and pass your button's clientId to RegisterDialog. 在内容页面上,将this.MasterPage转换为母版页的实际类名,然后将按钮的clientId传递给RegisterDialog。

--edited example -- -编辑示例-

For example your master page class name is CustomSiteMaster, you can write ((CustomSiteMaster)this.Master).RegisterDialog(button1.ClientID); 例如,您的母版页类名称为CustomSiteMaster,您可以编写((CustomSiteMaster)this.Master).RegisterDialog(button1.ClientID); -- end of edit -- -编辑结束-

It will work without problem 它将毫无问题地工作

Add to LinkButton OnClientClick property as below: 添加到LinkBut​​ton OnClientClick属性,如下所示:

<asp:LinkButton ID="Boton" runat="server" 
OnClientClick="$('#dialog').dialog('open'); return false;" >LinkButton</asp:LinkButton>

Also, if you want to select server-side control by id use following selector syntax: 另外,如果要通过id选择服务器端控件,请使用以下选择器语法:

$("#<%= ControlID.ClientID %>")

要在内容占位符中找到任何控件,请确保使用如下语法:

$('#ContentPlaceHolderId_ControlId')

jQuery UI is not being loaded because your src attribute has a additional space in it: jQuery UI未加载,因为您的src属性中还有一个额外的空间:

 <script type="text/javascript" src="js/jquery-ui- 1.8.16.custom.min.js"></script>

Just change to: 只需更改为:

<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>

And jQuery UI might load fine and you will stop receiving "the object does not support this property or method" messages. 而且jQuery UI可能会正常加载,您将停止接收"the object does not support this property or method"消息。

I have the solution to the "mistery". 我有解决“先生”的方法。 The problem is very simple. 问题很简单。 The references to .js files of jQuery-UI are in Conten1 placeholder, I put this two lines in Conten2 placeholder, this content has a javascript function in order to open the dialog. 对jQuery-UI的.js文件的引用在Conten1占位符中,我将这两行放在Conten2占位符中,此内容具有javascript函数,以打开对话框。

The complete and working code is the following: 完整且有效的代码如下:

<%@ Page Title="" Language="vb" AutoEventWireup="false"   MasterPageFile="~/privado/master/interior.master" CodeBehind="WebForm3.aspx.vb"   Inherits="ProyectoDemo.WebForm3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="headInterior" runat="server">
    <link type="text/css" href="Styles/jquery-ui-1.8.16.custom.css"    rel="stylesheet" />  
    <link href="Styles/addhunters.css" rel="stylesheet" type="text/css" />
    <%--<%@ MasterType  VirtualPath="~/privado/master/Interior.master"%>--%>
    <style type="text/css">
        /*demo page css*/
        body{ font: 62.5% "Trebuchet MS", sans-serif; margin: 50px;}
        .demoHeaders { margin-top: 2em; }
        #dialog_link {padding: .4em 1em .4em 20px;text-decoration:    none;position: relative;}
        #dialog_link span.ui-icon {margin: 0 5px 0 0;position:   absolute;left: .2em;top: 50%;margin-top: -8px;}
        ul#icons {margin: 0; padding: 0;}
        ul#icons li {margin: 2px; position: relative; padding: 4px 0;   cursor: pointer; float: left;  list-style: none;}
        ul#icons span.ui-icon {float: left; margin: 0 4px;}
    </style>    
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="contenidoInterior" runat="server">
    <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-   1.8.16.custom.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            alert('hola');
            $('#dialog').dialog({
                autoOpen: false,
                width: 600,
                height: 400,
                buttons: {
                    "Ok": function () {
                        $(this).dialog("close");
                    },
                    "Cancelar": function () {
                        $(this).dialog("close");
                    }
                }
            });

            $('#MainContent_contenidoInterior_Boton').click(function () {
                $('#dialog').dialog('open');
                return false;
            });
        });               
    </script>

    <div id="uno" >
        <h2 class="demoHeaders">Dialog</h2>        
        <asp:TextBox ID="TextBox1" runat="server" Text="prueba" ></asp:TextBox>
        <p><a href="#" id="dialog_link" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-newwin"></span>Open Dialog</a></p>              
        <asp:LinkButton ID="Boton" runat="server">LinkButton</asp:LinkButton>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <!-- ui-dialog -->
        <div id="dialog" title="Dialog Title">
            <p>Dialog text</p>            
        </div>  
    </div>

Thank you for your answers. 谢谢您的回答。

Javier 哈维尔

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

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