萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> 實現無刷新聯動例子匯總

實現無刷新聯動例子匯總

   最近在用asp.net做項目的時候,遇到需要實現無刷新聯動的需求,度娘了一下,這裡匯總一下幾個比較實用的例子,有需要的小伙伴可以參考下。

  Iframe實現無刷新聯動

  iframe的無刷新其實是局部刷新,狀態欄的滾動條還是會滾動,只是頁面不會閃爍,這是一種比較老的技術了,在處理的數據兩大的時候會比較慢,在本例中需要兩個頁面:index.aspx和frame.asapx,index.aspx用來顯示界面,其中有一個iframe標記,指向frame.aspx頁用來顯示結果

  index.aspx前台代碼

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="_Default" %>   <!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 id="Head1" runat="server"> <title>無標題頁</title>   <script type="text/javascript"> function Query() { var ddlpro = document.getElementById('ddlPro'); var pro = ddlpro.options[ddlpro.selectedIndex].innerText; if (pro != "") { document.getElementById("iframe1").src = "frame.aspx?Pro=" + pro; } }   </script>   </head> <body> <form id="form2" runat="server"> <div> <table border="1" cellpadding="3" cellspacing="0" width="600px"> <tr> <td colspan="2" align="center"> Iframe實現局部刷新 </td> </tr> <tr> <td> 省份名稱: </td> <td> <select id="ddlPro" style="width: 201px"> <option value="湖北">湖北</option> <option value="河北">河北</option> <option value="廣東">廣東</option> <option value="河南">河南</option> </select> <input id="Button3" type="button" value="查詢" onclick="Query()" /> </td> </tr> <tr> <td> 顯示城市列表 </td> <td> <iframe src="frame.aspx" style="text-align: center" id="iframe1" width="100%" height="100%" frameborder="0" scrolling="no" /> </td> </tr> </table> </div> </form> </body> </html>

  frame.aspx的前台代碼:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="frame.aspx.cs" Inherits="myframe" %>   <!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 id="Head1" runat="server"> <title>無標題頁</title> </head> <body> <form id="form2" runat="server"> <div> <asp:DropDownList ID="ddlCity" runat="server" Width="179px"> </asp:DropDownList> </div> </form> </body> </html>

  frame.aspx後台代碼:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;   public partial class myframe : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string pro = Request.QueryString["pro"]; switch (pro) { case "湖北": this.ddlCity.Items.Clear(); this.ddlCity.Items.Add("武漢"); this.ddlCity.Items.Add("黃岡"); this.ddlCity.Items.Add("黃石"); this.ddlCity.Items.Add("襄樊"); break; case "河北": this.ddlCity.Items.Clear(); this.ddlCity.Items.Add("石家莊"); this.ddlCity.Items.Add("唐山"); this.ddlCity.Items.Add("承德"); this.ddlCity.Items.Add("邯鄲"); break; case "廣東": this.ddlCity.Items.Clear(); this.ddlCity.Items.Add("廣州"); this.ddlCity.Items.Add("佛山"); this.ddlCity.Items.Add("深圳"
copyright © 萬盛學電腦網 all rights reserved