首页 > 语言 > JavaScript > 正文

实现无刷新联动例子汇总

2024-05-06 16:20:40
字体:大 中 小
来源:转载
供稿:网友

最近在用asp.net做项目的时候,遇到需要实现无刷新联动的需求,度娘了一下,这里汇总一下几个比较实用的例子,有需要的小伙伴可以参考下。

Iframe实现无刷新联动

iframe的无刷新其实是局部刷新,状态栏的滚动条还是会滚动,只是页面不会闪烁,这是一种比较老的技术了,在处理的数据两大的时候会比较慢,在本例中需要两个页面:index.aspx和frame.asapx,index.aspx用来显示界面,其中有一个iframe标记,指向frame.aspx页用来显示结果

index.aspx前台代码

 

 
  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>无标题页</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. } 
  16.  
  17. </script> 
  18.  
  19. </head> 
  20. <body> 
  21. <form id="form2" runat="server"> 
  22. <div> 
  23. <table border="1" cellpadding="3" cellspacing="0" width="600px"> 
  24. <tr> 
  25. <td colspan="2" align="center"> 
  26. Iframe实现局部刷新 
  27. </td> 
  28. </tr> 
  29. <tr> 
  30. <td> 
  31. 省份名称: 
  32. </td> 
  33. <td> 
  34. <select id="ddlPro" style="width: 201px"> 
  35. <option value="湖北">湖北</option> 
  36. <option value="河北">河北</option> 
  37. <option value="广东">广东</option> 
  38. <option value="河南">河南</option> 
  39. </select> 
  40. <input id="Button3" type="button" value="查询" onclick="Query()" /> 
  41. </td> 
  42. </tr> 
  43. <tr> 
  44. <td> 
  45. 显示城市列表 
  46. </td> 
  47. <td> 
  48. <iframe src="frame.aspx" style="text-align: center" id="iframe1" width="100%" 
  49. height="100%" frameborder="0" scrolling="no" /> 
  50. </td> 
  51. </tr> 
  52. </table> 
  53. </div> 
  54. </form> 
  55. </body> 
  56. </html> 

frame.aspx的前台代码:

 

 
  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>无标题页</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后台代码:

 

 
  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. { 
  14. protected void Page_Load(object sender, EventArgs e) 
  15. { 
  16. string pro = Request.QueryString["pro"]; 
  17. switch (pro) 
  18. { 
  19. case "湖北": 
  20. this.ddlCity.Items.Clear(); 
  21. this.ddlCity.Items.Add("武汉"); 
  22. this.ddlCity.Items.Add("黄冈"); 
  23. this.ddlCity.Items.Add("黄石"); 
  24. this.ddlCity.Items.Add("襄樊"); 
  25. break; 
  26. case "河北": 
  27. this.ddlCity.Items.Clear(); 
  28. this.ddlCity.Items.Add("石家庄"); 
  29. this.ddlCity.Items.Add("唐山"); 
  30. this.ddlCity.Items.Add("承德"); 
  31. this.ddlCity.Items.Add("邯郸"); 
  32. break; 
  33. case "广东": 
  34. this.ddlCity.Items.Clear(); 
  35. this.ddlCity.Items.Add("广州"); 
  36. this.ddlCity.Items.Add("佛山"); 
  37. this.ddlCity.Items.Add("深圳"); 
  38. this.ddlCity.Items.Add("珠海"); 
  39. break; 
  40. case "河南": 
  41. this.ddlCity.Items.Clear(); 
  42. this.ddlCity.Items.Add("郑州"); 
  43. this.ddlCity.Items.Add("新乡"); 
  44. this.ddlCity.Items.Add("安阳"); 
  45. this.ddlCity.Items.Add("信阳"); 
  46. break; 
  47.  
  48. } 
  49. } 
  50. } 

JavaScript无刷新联动

前台页面代码:

 

 
  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>无标题页</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. } 
  22. else { 
  23. document.getElementById("ddlCity").add(new Option(strcity, strcity)); 
  24. break; 
  25. } 
  26.  
  27. } 
  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. 脚本方法实现刷新 
  39. </td> 
  40. </tr> 
  41. <tr> 
  42. <td> 
  43. 选择省份: 
  44. </td> 
  45. <td> 
  46. <select id="ddlPro" style="width: 201px"> 
  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=" 查询" onclick="City()" /> 
  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> 

后台代码:

 

 
  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. { 
  15. protected void Page_Load(object sender, EventArgs e) 
  16. { 
  17. StringBuilder myscript = new StringBuilder(); 
  18. myscript.Append("function City() {/n"); 
  19. myscript.Append("var ddlpro=document.getElementById('ddlPro');/n"); 
  20. myscript.Append("var pro=ddlpro.options[ddlpro.selectedIndex].innerText;/n"); 
  21. //myscript.Append("var pro=document.getElementById('txtPro').value;/n"); 
  22. myscript.Append("switch(pro) { /n"); 
  23. myscript.Append("case '湖北':/n"); 
  24. myscript.Append("FillData('" + GetCityStr("湖北") + "');/n"); 
  25. myscript.Append("break;/n"); 
  26. myscript.Append("case '河北':/n"); 
  27. myscript.Append("FillData('" + GetCityStr("河北") + "');/n"); 
  28. myscript.Append("break;/n"); 
  29. myscript.Append("case '广东':/n"); 
  30. myscript.Append("FillData('" + GetCityStr("广东") + "');/n"); 
  31. myscript.Append("break;/n"); 
  32. myscript.Append("case '河南':/n"); 
  33. myscript.Append("FillData('" + GetCityStr("河南") + "');/n"); 
  34. myscript.Append("break;}/n"); 
  35. myscript.Append("}/n"); 
  36.  
  37. Page.ClientScript.RegisterClientScriptBlock(typeof(string), "city", myscript.ToString(), true); 
  38.  
  39. } 
  40.  
  41. private string GetCityStr(string pro) 
  42. { 
  43. string city = ""; 
  44. switch (pro) 
  45. { 
  46. case "湖北": 
  47. city = "武汉,黄冈,黄石,襄樊"; 
  48. break; 
  49. case "河北": 
  50. city = "石家庄,唐山,承德,邯郸"; 
  51. break; 
  52. case "广东": 
  53. city = "广州,佛山,深圳,珠海"; 
  54. break; 
  55. case "河南": 
  56. city = "郑州,新乡,安阳,信阳"; 
  57. break; 
  58. } 
  59. return city; 
  60. } 
  61. } 

CallBack无刷新联动

前台代码:

 

 
  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>无标题页</title> 
  7.  
  8. <script type="text/javascript"> 
  9. function FillData() 
  10. { 
  11. var ddlpro=document.getElementById('ddlPro'); 
  12. var pro=ddlpro.options[ddlpro.selectedIndex].value; 
  13. <% =ClientScript.GetCallbackEventReference(this,"pro","FillDll",null) %> 
  14. } 
  15.  
  16. function FillDll(strcity) 
  17. { 
  18. document.getElementById("ddlCity").options.length=0; 
  19. var indexofcity; 
  20. var city; 
  21. while(strcity.length>0) 
  22. { 
  23. indexofcity=strcity.indexOf(","); 
  24. if(indexofcity>0) 
  25. { 
  26. city=strcity.substring(0,indexofcity); 
  27.  
  28. strcity=strcity.substring(indexofcity+1); 
  29. document.getElementById("ddlCity").add(new Option(city,city)); 
  30. } 
  31. else 
  32. { 
  33. document.getElementById("ddlCity").add(new Option(strcity,strcity)); 
  34. break; 
  35. } 
  36.  
  37. } 
  38. } 
  39. </script> 
  40.  
  41. </head> 
  42. <body> 
  43. <form id="form2" runat="server"> 
  44. <div> 
  45. <table width="700px" border="1" cellpadding="5" cellspacing="0"> 
  46. <tr> 
  47. <td colspan="2" align="center"> 
  48. callback方法实现刷新 
  49. </td> 
  50. </tr> 
  51. <tr> 
  52. <td> 
  53. 选择省份: 
  54. </td> 
  55. <td> 
  56. <select id="ddlPro" style="width: 200px"> 
  57. <option value="湖北">湖北</option> 
  58. <option value="河北">河北</option> 
  59. <option value="广东">广东</option> 
  60. <option value="河南">河南</option> 
  61. </select> 
  62. <input id="btnQuery" type="button" value=" 查询" onclick="FillData()" /> 
  63. </td> 
  64. </tr> 
  65. <tr> 
  66. <td> 
  67. 城市: 
  68. </td> 
  69. <td> 
  70. <asp:DropDownList ID="ddlCity" runat="server" Width="201px"> 
  71. </asp:DropDownList> 
  72. </td> 
  73. </tr> 
  74. </table> 
  75. </div> 
  76. </form> 
  77. </body> 
  78. </html> 

后台代码:

 

 
  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. { 
  14. private string _data; 
  15. protected void Page_Load(object sender, EventArgs e) 
  16. { 
  17.  
  18. } 
  19.  
  20. ICallbackEventHandler 成员 
  21. } 

Ajax无刷新联动

该例子也要用到两个页面:oec203index.aspx和datapage.aspx. datapage.aspx主要用来回送要显示的数据

.aspx页面前台代码:

 

 
  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>无标题页</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.  
  19. function statechange() { 
  20. if (xmlhttp.readystate == 4) { 
  21. if (xmlhttp.status == 200) { 
  22. FillData(xmlhttp.responseText); 
  23. } 
  24. } 
  25. } 
  26. function FillData(strcity) { 
  27. document.getElementById("ddlCity").options.length = 0; 
  28. var indexofcity; 
  29. var city; 
  30. while (strcity.length > 0) { 
  31. indexofcity = strcity.indexOf(","); 
  32. if (indexofcity > 0) { 
  33. city = strcity.substring(0, indexofcity); 
  34. strcity = strcity.substring(indexofcity + 1); 
  35. document.getElementById("ddlCity").add(new Option(city, city)); 
  36. } 
  37. else { 
  38. document.getElementById("ddlCity").add(new Option(strcity, strcity)); 
  39. break; 
  40. } 
  41. } 
  42. } 
  43. </script> 
  44.  
  45. </head> 
  46. <body> 
  47. <form id="form2" runat="server"> 
  48. <div> 
  49. <table width="700px" border="1" cellpadding="5" cellspacing="0"> 
  50. <tr> 
  51. <td colspan="2" align="center"> 
  52. ajax方法实现刷新 
  53. </td> 
  54. </tr> 
  55. <tr> 
  56. <td> 
  57. 选择省份: 
  58. </td> 
  59. <td> 
  60. <select id="ddlPro" style="width: 201px"> 
  61. <option value="湖北">湖北</option> 
  62. <option value="河北">河北</option> 
  63. <option value="广东">广东</option> 
  64. <option value="河南">河南</option> 
  65. </select> 
  66. <input id="btnQuery" type="button" value=" 查询" onclick="getData()" /> 
  67. </td> 
  68. </tr> 
  69. <tr> 
  70. <td> 
  71. 城市: 
  72. </td> 
  73. <td> 
  74. <asp:DropDownList ID="ddlCity" runat="server" Width="201px"> 
  75. </asp:DropDownList> 
  76. </td> 
  77. </tr> 
  78. </table> 
  79. </div> 
  80. </form> 
  81. </body> 
  82. </html> 

datapage.aspx后台代码:

 

 
  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. { 
  14. protected void Page_Load(object sender, EventArgs e) 
  15. { 
  16. string pro = Request.QueryString["pro"]; 
  17. Response.Clear(); 
  18. switch (pro) 
  19. { 
  20. case "湖北": 
  21. Response.Write("武汉,黄冈,黄石,襄樊"); 
  22. break; 
  23. case "河北": 
  24. Response.Write("石家庄,唐山,承德,邯郸"); 
  25. break; 
  26. case "广东": 
  27. Response.Write("广州,佛山,深圳,珠海"); 
  28. break; 
  29. case "河南": 
  30. Response.Write("郑州,新乡,安阳,信阳"); 
  31. break; 
  32. } 
  33. } 
  34. } 

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选