2011-02-17 94 views
0

我刚刚开始学习jQuery,并希望将内容从单独的.aspx页面动态加载到div中。从这里使用示例:http://www.asp.net/ajaxLibrary/jquery_webforms_dynamic_load.ashx?HL=var动态内容w/jQuery

然而,它似乎并没有回应,我可能错过了这一点。下面是代码/脚本在我的.aspx页面中:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
<script src="Scripts/jquery-1.5.js" type="text/javascript"></script>  
<script type="text/javascript"> 

$(document).ready(function() { 
    // External ASPX Page calling 
    $("#btn_Submit").click(loadDynamic); 
}); 

function loadDynamic() { 

    $("#dynamicResults").load("ResultsView.aspx", 
     {name: $("#cbox_User").val() }, 
     function (content) { 
      $(this).hide().fadeIn("slow"); 
       return false; 
     }); 
} 

<Header> 
QUERY VIEW 
</Header> 

<Content> 
    <div style="float:right; height:154px; width: 947px; margin-left: 0px; background-color: #E0E0E0;"> 
     <br /> 
     <asp:Label ID="Label2" runat="server" Text="Select a User:" 
        Style="margin-left:28px" ></asp:Label> 

        <asp:ComboBox ID="cbox_User" runat="server" AutoCompleteMode="SuggestAppend"> 
        </asp:ComboBox> 

       <asp:Label ID="Label3" runat="server" Text="Select a Month:" 
           Style="margin-left:28px" ></asp:Label> 
      <asp:TextBox ID="txt_Date" runat="server"></asp:TextBox> 
       <asp:CalendarExtender ID="CalendarExtender1" runat="server" 
          TargetControlID="txt_Date" 
          Format="MMMM yyyy" 
          OnClientShown="onCalendarShown" 
          OnClientHidden="onCalendarHidden" 
          BehaviorID="calendar1" > 
       </asp:CalendarExtender> 
       <asp:Button ID="btn_Submit" runat="server" Text="Submit" Style="margin-left:28px" onclick="Btn_Submit_Click" /> 
</div> 
</Content> 


<Header> 
RESULTS VIEW 
</Header> 

<Content> 
    <div id="dynamicResults"> 
    </div> 
    <div style="border-style: none; height:340px; width: 770px; position:relative; top: 10px; left: -2px;"> 
    <asp:GridView ID="ResultsView" runat="server" CellPadding="3" 
     ForeColor="Black" GridLines="None" AllowPaging="False" 
     Visible="False" 
     Height="318px" style="margin-left: 32px; margin-top: 2px;" Width="718px" 
     BackColor="White" BorderColor="#999999" BorderStyle="Solid" 
     BorderWidth="1px"> 
     </asp:GridView> 
    </div> 
</Content> 

而在第二个的.aspx页面,其中包含我一个div我只想动态负载:

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <div style="background-color:#E0E0E0; border-style: ridge none none none; border-  width: thin; border-color: #B3B3B3; height:120px; width: 770px; position:relative;  top: 10px; left: 8px;"> 
      <asp:Label ID="lbl_Header" runat="server" Text="User Information:"></asp:Label> 
    </div> 
</html> 
+1

您能否接受其他一些问题的答案?谢谢! – SquidScareMe 2011-02-17 19:16:24

+0

那是绿色的复选标记吗? – Sean 2011-02-17 19:20:46

回答

1

看看load方法。

这里是从页面的一个示例:

加载页面片段的.load() 方法,不像$获得(),允许我们 指定远程 文档的一部分是插入。这是使用 url参数的特殊语法实现的 。如果字符串中包含一个或多个空格 字符,则 字符串的部分跟在 后面,则假定第一个空格为 jQuery选择器,用于确定要加载的 内容。

我们可以修改上面的例子 仅使用文档的一部分是 牵强:

$('#result').load('ajax/test.html #container'); 

当这种方法执行,它 检索的 AJAX/test.html中的内容,但随后jQuery解析返回的文档 以查找容器ID为 的元素。这个 元素及其内容是 插入元素的ID为 的结果,并且 的其余部分将被丢弃。

jQuery使用浏览器的.innerHTML 属性来解析检索 文档,并将其插入到 当前文档。在此过程中, 浏览器通常会过滤来自 的文档,如 或元素。因此,由.load()检索的 元素可能不是 与由 浏览器直接检索到文档 完全相同。

编辑:刚才注意到您的功能loadDynamic()你正在试图获得控制cbox_User像这样的值:

$("#cbox_User").val() 

但是,因为它是一个服务器端控件,你需要得到这样的值:

$("#<%=cbox_User.ClientID%.").val() 

这是因为.NET为ASP.NET控件提供了不同于您指定的ID。

希望这会有所帮助。