2012-10-31 51 views
1

我需要打开一个页面,就像模态对话框。我在网上找到一个例子,但它不起作用。如何在ASP.NET中打开一个像aspx页面的aspx页面

在主网页我有这样的代码:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <asp:Button Text ="Add New Course" runat="server" ID="btnAddCourse" OnClientClick="showPanel('dialog');return false;"/> 
     <script type="text/javascript"> 
      function showPanel(panelID) { 
       $panel = $('#' + panelID); 
       $.ajax({ 
        url: "/AddNew.aspx", 
        type: "GET", 
        dataType: "html", 
        async: false, 
        data: { "param": "abcd" 
        }, 
        success: function (obj) { 
         // obj will contain the complete contents of the page requested 
         // use jquery to extract just the html inside the body tag 
         $content = $(obj).find('body').html(); 
         // then update the dialog contents with this and show it 
         $panel.html($content); 
         $panel.dialog(); 
        } 
       }); 
      } 
    </script> 
<div id="dialog"> 
</div> 
</asp:Content> 

当我按一下按钮我需要打开下面。我的页面收到一个错误告诉元素$不recognized.I不确切地知道谁是元素面板。必须添加面板控件,但是在哪里?

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddNew.aspx.cs" Inherits="WebApplicationDialog.AddNew" %> 

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

     <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
    <title>Add New Course </title> 
</head> 
<body> 
<form > 
<div id="div1"> 
<table> 
<tr><td colspan="3"> <asp:Label ID="lblCourse" runat="server" Text="Add New Course"></asp:Label></td></tr> 
<tr><td colspan="3"> </td></tr> 
<tr> 
    <td style="width:40%"> <asp:Label ID="lblName" runat="server" Text="Course Name" ></asp:Label></td> 
    <td style="width:20%"> </td> 
    <td style="width:40%"> 
     <input id="txtName" type="text" /> 
    </td> 
</tr> 
<tr> 
    <td style="width:40%"> <asp:Label ID="lblDescription" runat="server" Text="Description" ></asp:Label></td> 
    <td style="width:20%"> </td> 
    <td style="width:40%"> 
     <input id="txtDescription" type="text" /> 

    </td> 
</tr> 
<tr><td colspan="3" style="float:right"> 
<input value ="Save" id="btnSave" type="submit" /> </td></tr> 
</table> 
</div> 

</form> 
</body> 
</html> 

有人可以帮我用这段代码,使它有效吗? 谢谢

+0

这可能不会工作无论如何,由于“成功”函数是异步执行的,因此并不总是需要知道$面板。最好在成功主体中查找面板 – Chris

+0

我们是否也可以拥有主页面的头部部分,以查看引用库的方式 – ginja

回答

0

它看起来像你正在尝试使用jQuery的网页内容加载到一个面板,但如果我在读你的代码对你可能没有包括JQuery库,您需要将这行代码包含在您的html代码的<head>中。您还使用jQueryUI的对话框,需要引用jQueryUI的库

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> 

对于实例

<head> 
    <title>Page Title</title> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> 
<head> 

编辑:我也注意到你正在使用的对话框,这是在JQuery用户界面,pleasse请确保您引用JQuery的UI工具包以及

编辑:掠夺了你的代码的jsfiddle:http://jsfiddle.net/EhPk7/1/ 这似乎为我工作

+1

由于TS正在使用母版页,我认为jQuery ref就在母版页中。还是一句好话! – Chris

+0

是的,这可能是真的,但基于他得到的错误,我仍然猜测这是问题,不能确定没有看到母版页代码 – ginja

+0

我添加这个引用,我没有错误,但它当我点击按钮:( – user1577242

0

您需要添加jquery lib参考。 在页面的顶部:

<script src="http://code.jquery.com/jquery-1.8.2.min.js"/> 
+0

由于TS正在使用母版页,因此我认为jQuery ref是其主人页。还是一句好话! – Chris

+0

无论他使用什么,在他的脚本执行jauery不加载时。 – Anri

+0

我在主页面的头部添加了这一行,但它仍然告诉我$是未定义的 – user1577242

0

考虑加入jQuery脚本您的网页上引用,您正在使用jQuery代码之上。

此外,添加引用jQuery UI

Download jQuery

0

包括jQuery。 更改此行 - > $ panel = $('#'+ panelID); to - > $ panel = $('#div1');并看看它是否有效。

0

在jQueryUI对话框中有一个名为modal的布尔属性/设置,这将创建一个模式类型的对话框。

我自己用的这个:

d.dialog({ 
      autoOpen: true, 
      closeOnEscape: false, 
      closeText: '', 
      modal: true, 
      draggable: false, 
      resizable: false, 
      width: (window.document.width * 0.75), 
      dialogClass: 'popup loading', 
      title: 'title'), 

     }); 

当您缠绕这种功能在

$(document).ready(function() { 

}) 

应该弹出的瞬间DOM是准备..

+0

我应该在哪里复制这段代码?我在哪里告诉哪个页面? – user1577242

+0

在主页面或在对话框页面? – user1577242

+0

这段代码应该在主页面,因为这个页面需要打开对话框。 – Chris