DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> 實現無刷新聯動例子匯總
實現無刷新聯動例子匯總
編輯: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("深圳"); 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;   } } }

  JavaScript無刷新聯動

  前台頁面代碼:

  ?

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 57 58 59 60 61 62 63 64 65 66 67 68 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="jacascript_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 FillData(strcity) {   document.getElementById("ddlCity").options.length = 0; var indexofcity; var city; while (strcity.length > 0) { indexofcity = strcity.indexOf(","); if (indexofcity > 0) { city = strcity.substring(0, indexofcity);   strcity = strcity.substring(indexofcity + 1); document.getElementById("ddlCity").add(new Option(city, city)); } else { document.getElementById("ddlCity").add(new Option(strcity, strcity)); break; }   } } </script>   </head> <body> <form id="form2" runat="server"> <div> <table width="700px" border="1" cellpadding="5" cellspacing="0"> <tr> <td colspan="2" align="center"> 腳本方法實現刷新 </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="btnQuery" type="button" value=" 查詢" onclick="City()" /> </td> </tr> <tr> <td> 城市: </td> <td> <asp:DropDownList ID="ddlCity" runat="server" Width="201px"> </asp:DropDownList> </td> </tr> </table> </div> </form> </body> </html>

  後台代碼:

  ?

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 57 58 59 60 61 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; using System.Text;   public partial class jacascript_Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { StringBuilder myscript = new StringBuilder(); myscript.Append("function City() {n"); myscript.Append("var ddlpro=document.getElementById('ddlPro');n"); myscript.Append("var pro=ddlpro.options[ddlpro.selectedIndex].innerText;n"); //myscript.Append("var pro=document.getElementById('txtPro').value;n"); myscript.Append("switch(pro) { n"); myscript.Append("case '湖北':n"); myscript.Append("FillData('" + GetCityStr("湖北") + "');n"); myscript.Append("break;n"); myscript.Append("case '河北':n"); myscript.Append("FillData('" + GetCityStr("河北") + "');n"); myscript.Append("break;n"); myscript.Append("case '廣東':n"); myscript.Append("FillData('" + GetCityStr("廣東") + "');n"); myscript.Append("break;n"); myscript.Append("case '河南':n"); myscript.Append("FillData('" + GetCityStr("河南") + "');n"); myscript.Append("break;}n"); myscript.Append("}n");   Page.ClientScript.RegisterClientScriptBlock(typeof(string), "city", myscript.ToString(), true);   }   private string GetCityStr(string pro) { string city = ""; switch (pro) { case "湖北": city = "武漢,黃岡,黃石,襄樊"; break; case "河北": city = "石家莊,唐山,承德,邯鄲"; break; case "廣東": city = "廣州,佛山,深圳,珠海"; break; case "河南": city = "鄭州,新鄉,安陽,信陽"; break; } return city; } }

  CallBack無刷新聯動

  前台代碼:

  ?

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="callback_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 FillData() { var ddlpro=document.getElementById('ddlPro'); var pro=ddlpro.options[ddlpro.selectedIndex].value; <% =ClientScript.GetCallbackEventReference(this,"pro","FillDll",null) %> }   function FillDll(strcity) { document.getElementById("ddlCity").options.length=0; var indexofcity; var city; while(strcity.length>0) { indexofcity=strcity.indexOf(","); if(indexofcity>0) { city=strcity.substring(0,indexofcity);   strcity=strcity.substring(indexofcity+1); document.getElementById("ddlCity").add(new Option(city,city)); } else { document.getElementById("ddlCity").add(new Option(strcity,strcity)); break; }   } } </script>   </head> <body> <form id="form2" runat="server"> <div> <table width="700px" border="1" cellpadding="5" cellspacing="0"> <tr> <td colspan="2" align="center"> callback方法實現刷新 </td> </tr> <tr> <td> 選擇省份: </td> <td> <select id="ddlPro" style="width: 200px"> <option value="湖北">湖北</option> <option value="河北">河北</option> <option value="廣東">廣東</option> <option value="河南">河南</option> </select> <input id="btnQuery" type="button" value=" 查詢" onclick="FillData()" /> </td> </tr> <tr> <td> 城市: </td> <td> <asp:DropDownList ID="ddlCity" runat="server" Width="201px"> </asp:DropDownList> </td> </tr> </table> </div> </form> </body> </html>

  後台代碼:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 callback_Default : System.Web.UI.Page,ICallbackEventHandler { private string _data; protected void Page_Load(object sender, EventArgs e) {   }   ICallbackEventHandler 成員 }

  Ajax無刷新聯動

  該例子也要用到兩個頁面:oec203index.aspx和datapage.aspx. datapage.aspx主要用來回送要顯示的數據

  .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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="ajax_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"> var xmlhttp; function getData() { var ddlpro = document.getElementById("ddlPro"); var pro = ddlpro.options[ddlpro.selectedIndex].innerText; xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.onreadystatechange = statechange; xmlhttp.Open("GET", "datapage.aspx?pro=" + pro, true); xmlhttp.Send(); }   function statechange() { if (xmlhttp.readystate == 4) { if (xmlhttp.status == 200) { FillData(xmlhttp.responseText); } } } function FillData(strcity) { document.getElementById("ddlCity").options.length = 0; var indexofcity; var city; while (strcity.length > 0) { indexofcity = strcity.indexOf(","); if (indexofcity > 0) { city = strcity.substring(0, indexofcity); strcity = strcity.substring(indexofcity + 1); document.getElementById("ddlCity").add(new Option(city, city)); } else { document.getElementById("ddlCity").add(new Option(strcity, strcity)); break; } } } </script>   </head> <body> <form id="form2" runat="server"> <div> <table width="700px" border="1" cellpadding="5" cellspacing="0"> <tr> <td colspan="2" align="center"> ajax方法實現刷新 </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="btnQuery" type="button" value=" 查詢" onclick="getData()" /> </td> </tr> <tr> <td> 城市: </td> <td> <asp:DropDownList ID="ddlCity" runat="server" Width="201px"> </asp:DropDownList> </td> </tr> </table> </div> </form> </body> </html>

  datapage.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 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 ajax_datapage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string pro = Request.QueryString["pro"]; Response.Clear(); switch (pro) { case "湖北": Response.Write("武漢,黃岡,黃石,襄樊"); break; case "河北": Response.Write("石家莊,唐山,承德,邯鄲"); break; case "廣東": Response.Write("廣州,佛山,深圳,珠海"); break; case "河南": Response.Write("鄭州,新鄉,安陽,信陽"); break; } } }

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved