国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > JavaScript > 正文

實(shí)現(xiàn)無(wú)刷新聯(lián)動(dòng)例子匯總

2024-05-06 16:20:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近在用asp.net做項(xiàng)目的時(shí)候,遇到需要實(shí)現(xiàn)無(wú)刷新聯(lián)動(dòng)的需求,度娘了一下,這里匯總一下幾個(gè)比較實(shí)用的例子,有需要的小伙伴可以參考下。

Iframe實(shí)現(xiàn)無(wú)刷新聯(lián)動(dòng)

iframe的無(wú)刷新其實(shí)是局部刷新,狀態(tài)欄的滾動(dòng)條還是會(huì)滾動(dòng),只是頁(yè)面不會(huì)閃爍,這是一種比較老的技術(shù)了,在處理的數(shù)據(jù)兩大的時(shí)候會(huì)比較慢,在本例中需要兩個(gè)頁(yè)面:index.aspx和frame.asapx,index.aspx用來(lái)顯示界面,其中有一個(gè)iframe標(biāo)記,指向frame.aspx頁(yè)用來(lái)顯示結(jié)果

index.aspx前臺(tái)代碼

 

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

frame.aspx的前臺(tái)代碼:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="frame.aspx.cs" Inherits="myframe" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. <head id="Head1" runat="server"
  6. <title>無(wú)標(biāo)題頁(yè)</title> 
  7. </head> 
  8. <body> 
  9. <form id="form2" runat="server"
  10. <div> 
  11. <asp:DropDownList ID="ddlCity" runat="server" Width="179px"
  12. </asp:DropDownList> 
  13. </div> 
  14. </form> 
  15. </body> 
  16. </html> 

frame.aspx后臺(tái)代碼:

 

 
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Collections; 
  5. using System.Web; 
  6. using System.Web.Security; 
  7. using System.Web.UI; 
  8. using System.Web.UI.WebControls; 
  9. using System.Web.UI.WebControls.WebParts; 
  10. using System.Web.UI.HtmlControls; 
  11.  
  12. public partial class myframe : System.Web.UI.Page 
  13. protected void Page_Load(object sender, EventArgs e) 
  14. string pro = Request.QueryString["pro"]; 
  15. switch (pro) 
  16. case "湖北"
  17. this.ddlCity.Items.Clear(); 
  18. this.ddlCity.Items.Add("武漢"); 
  19. this.ddlCity.Items.Add("黃岡"); 
  20. this.ddlCity.Items.Add("黃石"); 
  21. this.ddlCity.Items.Add("襄樊"); 
  22. break
  23. case "河北"
  24. this.ddlCity.Items.Clear(); 
  25. this.ddlCity.Items.Add("石家莊"); 
  26. this.ddlCity.Items.Add("唐山"); 
  27. this.ddlCity.Items.Add("承德"); 
  28. this.ddlCity.Items.Add("邯鄲"); 
  29. break
  30. case "廣東"
  31. this.ddlCity.Items.Clear(); 
  32. this.ddlCity.Items.Add("廣州"); 
  33. this.ddlCity.Items.Add("佛山"); 
  34. this.ddlCity.Items.Add("深圳"); 
  35. this.ddlCity.Items.Add("珠海"); 
  36. break
  37. case "河南"
  38. this.ddlCity.Items.Clear(); 
  39. this.ddlCity.Items.Add("鄭州"); 
  40. this.ddlCity.Items.Add("新鄉(xiāng)"); 
  41. this.ddlCity.Items.Add("安陽(yáng)"); 
  42. this.ddlCity.Items.Add("信陽(yáng)"); 
  43. break
  44.  

JavaScript無(wú)刷新聯(lián)動(dòng)

前臺(tái)頁(yè)面代碼:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="jacascript_Default" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. <head id="Head1" runat="server"
  6. <title>無(wú)標(biāo)題頁(yè)</title> 
  7.  
  8. <script type="text/javascript"
  9. function FillData(strcity) { 
  10.  
  11. document.getElementById("ddlCity").options.length = 0; 
  12. var indexofcity; 
  13. var city; 
  14. while (strcity.length > 0) { 
  15. indexofcity = strcity.indexOf(","); 
  16. if (indexofcity > 0) { 
  17. city = strcity.substring(0, indexofcity); 
  18.  
  19. strcity = strcity.substring(indexofcity + 1); 
  20. document.getElementById("ddlCity").add(new Option(city, city)); 
  21. else { 
  22. document.getElementById("ddlCity").add(new Option(strcity, strcity)); 
  23. break
  24.  
  25. </script> 
  26.  
  27. </head> 
  28. <body> 
  29. <form id="form2" runat="server"
  30. <div> 
  31. <table width="700px" border="1" cellpadding="5" cellspacing="0"
  32. <tr> 
  33. <td colspan="2" align="center"
  34. 腳本方法實(shí)現(xiàn)刷新 
  35. </td> 
  36. </tr> 
  37. <tr> 
  38. <td> 
  39. 選擇省份: 
  40. </td> 
  41. <td> 
  42. <select id="ddlPro" style="width: 201px"
  43. <option value="湖北">湖北</option> 
  44. <option value="河北">河北</option> 
  45. <option value="廣東">廣東</option> 
  46. <option value="河南">河南</option> 
  47. </select> 
  48. <input id="btnQuery" type="button" value=" 查詢(xún)" onclick="City()" /> 
  49. </td> 
  50. </tr> 
  51. <tr> 
  52. <td> 
  53. 城市: 
  54. </td> 
  55. <td> 
  56. <asp:DropDownList ID="ddlCity" runat="server" Width="201px"
  57. </asp:DropDownList> 
  58. </td> 
  59. </tr> 
  60. </table> 
  61. </div> 
  62. </form> 
  63. </body> 
  64. </html> 

后臺(tái)代碼:

 

 
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Collections; 
  5. using System.Web; 
  6. using System.Web.Security; 
  7. using System.Web.UI; 
  8. using System.Web.UI.WebControls; 
  9. using System.Web.UI.WebControls.WebParts; 
  10. using System.Web.UI.HtmlControls; 
  11. using System.Text; 
  12.  
  13. public partial class jacascript_Default : System.Web.UI.Page 
  14. protected void Page_Load(object sender, EventArgs e) 
  15. StringBuilder myscript = new StringBuilder(); 
  16. myscript.Append("function City() {/n"); 
  17. myscript.Append("var ddlpro=document.getElementById('ddlPro');/n"); 
  18. myscript.Append("var pro=ddlpro.options[ddlpro.selectedIndex].innerText;/n"); 
  19. //myscript.Append("var pro=document.getElementById('txtPro').value;/n"); 
  20. myscript.Append("switch(pro) { /n"); 
  21. myscript.Append("case '湖北':/n"); 
  22. myscript.Append("FillData('" + GetCityStr("湖北") + "');/n"); 
  23. myscript.Append("break;/n"); 
  24. myscript.Append("case '河北':/n"); 
  25. myscript.Append("FillData('" + GetCityStr("河北") + "');/n"); 
  26. myscript.Append("break;/n"); 
  27. myscript.Append("case '廣東':/n"); 
  28. myscript.Append("FillData('" + GetCityStr("廣東") + "');/n"); 
  29. myscript.Append("break;/n"); 
  30. myscript.Append("case '河南':/n"); 
  31. myscript.Append("FillData('" + GetCityStr("河南") + "');/n"); 
  32. myscript.Append("break;}/n"); 
  33. myscript.Append("}/n"); 
  34.  
  35. Page.ClientScript.RegisterClientScriptBlock(typeof(string), "city", myscript.ToString(), true); 
  36.  
  37.  
  38. private string GetCityStr(string pro) 
  39. string city = ""
  40. switch (pro) 
  41. case "湖北"
  42. city = "武漢,黃岡,黃石,襄樊"
  43. break
  44. case "河北"
  45. city = "石家莊,唐山,承德,邯鄲"
  46. break
  47. case "廣東"
  48. city = "廣州,佛山,深圳,珠海"
  49. break
  50. case "河南"
  51. city = "鄭州,新鄉(xiāng),安陽(yáng),信陽(yáng)"
  52. break
  53. return city; 

CallBack無(wú)刷新聯(lián)動(dòng)

前臺(tái)代碼:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="callback_Default" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. <head id="Head1" runat="server"
  6. <title>無(wú)標(biāo)題頁(yè)</title> 
  7.  
  8. <script type="text/javascript"
  9. function FillData() 
  10. var ddlpro=document.getElementById('ddlPro'); 
  11. var pro=ddlpro.options[ddlpro.selectedIndex].value; 
  12. <% =ClientScript.GetCallbackEventReference(this,"pro","FillDll",null) %> 
  13.  
  14. function FillDll(strcity) 
  15. document.getElementById("ddlCity").options.length=0; 
  16. var indexofcity; 
  17. var city; 
  18. while(strcity.length>0) 
  19. indexofcity=strcity.indexOf(","); 
  20. if(indexofcity>0) 
  21. city=strcity.substring(0,indexofcity); 
  22.  
  23. strcity=strcity.substring(indexofcity+1); 
  24. document.getElementById("ddlCity").add(new Option(city,city)); 
  25. else 
  26. document.getElementById("ddlCity").add(new Option(strcity,strcity)); 
  27. break
  28.  
  29. </script> 
  30.  
  31. </head> 
  32. <body> 
  33. <form id="form2" runat="server"
  34. <div> 
  35. <table width="700px" border="1" cellpadding="5" cellspacing="0"
  36. <tr> 
  37. <td colspan="2" align="center"
  38. callback方法實(shí)現(xiàn)刷新 
  39. </td> 
  40. </tr> 
  41. <tr> 
  42. <td> 
  43. 選擇省份: 
  44. </td> 
  45. <td> 
  46. <select id="ddlPro" style="width: 200px"
  47. <option value="湖北">湖北</option> 
  48. <option value="河北">河北</option> 
  49. <option value="廣東">廣東</option> 
  50. <option value="河南">河南</option> 
  51. </select> 
  52. <input id="btnQuery" type="button" value=" 查詢(xún)" onclick="FillData()" /> 
  53. </td> 
  54. </tr> 
  55. <tr> 
  56. <td> 
  57. 城市: 
  58. </td> 
  59. <td> 
  60. <asp:DropDownList ID="ddlCity" runat="server" Width="201px"
  61. </asp:DropDownList> 
  62. </td> 
  63. </tr> 
  64. </table> 
  65. </div> 
  66. </form> 
  67. </body> 
  68. </html> 

后臺(tái)代碼:

 

 
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Collections; 
  5. using System.Web; 
  6. using System.Web.Security; 
  7. using System.Web.UI; 
  8. using System.Web.UI.WebControls; 
  9. using System.Web.UI.WebControls.WebParts; 
  10. using System.Web.UI.HtmlControls; 
  11.  
  12. public partial class callback_Default : System.Web.UI.Page,ICallbackEventHandler 
  13. private string _data; 
  14. protected void Page_Load(object sender, EventArgs e) 
  15.  
  16.  
  17. ICallbackEventHandler 成員 

Ajax無(wú)刷新聯(lián)動(dòng)

該例子也要用到兩個(gè)頁(yè)面:oec203index.aspx和datapage.aspx. datapage.aspx主要用來(lái)回送要顯示的數(shù)據(jù)

.aspx頁(yè)面前臺(tái)代碼:

 

 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="ajax_Default" %> 
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  4. <html xmlns="http://www.w3.org/1999/xhtml"
  5. <head id="Head1" runat="server"
  6. <title>無(wú)標(biāo)題頁(yè)</title> 
  7.  
  8. <script type="text/javascript"
  9. var xmlhttp; 
  10. function getData() { 
  11. var ddlpro = document.getElementById("ddlPro"); 
  12. var pro = ddlpro.options[ddlpro.selectedIndex].innerText; 
  13. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
  14. xmlhttp.onreadystatechange = statechange; 
  15. xmlhttp.Open("GET""datapage.aspx?pro=" + pro, true); 
  16. xmlhttp.Send(); 
  17.  
  18. function statechange() { 
  19. if (xmlhttp.readystate == 4) { 
  20. if (xmlhttp.status == 200) { 
  21. FillData(xmlhttp.responseText); 
  22. function FillData(strcity) { 
  23. document.getElementById("ddlCity").options.length = 0; 
  24. var indexofcity; 
  25. var city; 
  26. while (strcity.length > 0) { 
  27. indexofcity = strcity.indexOf(","); 
  28. if (indexofcity > 0) { 
  29. city = strcity.substring(0, indexofcity); 
  30. strcity = strcity.substring(indexofcity + 1); 
  31. document.getElementById("ddlCity").add(new Option(city, city)); 
  32. else { 
  33. document.getElementById("ddlCity").add(new Option(strcity, strcity)); 
  34. break
  35. </script> 
  36.  
  37. </head> 
  38. <body> 
  39. <form id="form2" runat="server"
  40. <div> 
  41. <table width="700px" border="1" cellpadding="5" cellspacing="0"
  42. <tr> 
  43. <td colspan="2" align="center"
  44. ajax方法實(shí)現(xiàn)刷新 
  45. </td> 
  46. </tr> 
  47. <tr> 
  48. <td> 
  49. 選擇省份: 
  50. </td> 
  51. <td> 
  52. <select id="ddlPro" style="width: 201px"
  53. <option value="湖北">湖北</option> 
  54. <option value="河北">河北</option> 
  55. <option value="廣東">廣東</option> 
  56. <option value="河南">河南</option> 
  57. </select> 
  58. <input id="btnQuery" type="button" value=" 查詢(xún)" onclick="getData()" /> 
  59. </td> 
  60. </tr> 
  61. <tr> 
  62. <td> 
  63. 城市: 
  64. </td> 
  65. <td> 
  66. <asp:DropDownList ID="ddlCity" runat="server" Width="201px"
  67. </asp:DropDownList> 
  68. </td> 
  69. </tr> 
  70. </table> 
  71. </div> 
  72. </form> 
  73. </body> 
  74. </html> 

datapage.aspx后臺(tái)代碼:

 

 
  1. using System; 
  2. using System.Data; 
  3. using System.Configuration; 
  4. using System.Collections; 
  5. using System.Web; 
  6. using System.Web.Security; 
  7. using System.Web.UI; 
  8. using System.Web.UI.WebControls; 
  9. using System.Web.UI.WebControls.WebParts; 
  10. using System.Web.UI.HtmlControls; 
  11.  
  12. public partial class ajax_datapage : System.Web.UI.Page 
  13. protected void Page_Load(object sender, EventArgs e) 
  14. string pro = Request.QueryString["pro"]; 
  15. Response.Clear(); 
  16. switch (pro) 
  17. case "湖北"
  18. Response.Write("武漢,黃岡,黃石,襄樊"); 
  19. break
  20. case "河北"
  21. Response.Write("石家莊,唐山,承德,邯鄲"); 
  22. break
  23. case "廣東"
  24. Response.Write("廣州,佛山,深圳,珠海"); 
  25. break
  26. case "河南"
  27. Response.Write("鄭州,新鄉(xiāng),安陽(yáng),信陽(yáng)"); 
  28. break

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 本溪| 中牟县| 新安县| 克山县| 新安县| 沁水县| 中卫市| 青州市| 蒲城县| 上蔡县| 偃师市| 镇江市| 宜春市| 化隆| 新乐市| 宁德市| 邵东县| 屏东县| 云南省| 深水埗区| 河间市| 西吉县| 盱眙县| 旬阳县| 上虞市| 营山县| 漾濞| 高唐县| 山阴县| 郎溪县| 聂拉木县| 乾安县| 阳春市| 拉萨市| 尚志市| 土默特左旗| 贵德县| 六枝特区| 高淳县| 昭苏县| 大安市|