首页 > 语言 > 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. </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实现局部刷新 
  25. </td> 
  26. </tr> 
  27. <tr> 
  28. <td> 
  29. 省份名称: 
  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="查询" 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的前台代码:

 

 
  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. 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("新乡"); 
  41. this.ddlCity.Items.Add("安阳"); 
  42. this.ddlCity.Items.Add("信阳"); 
  43. break
  44.  

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. 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. 脚本方法实现刷新 
  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=" 查询" 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> 

后台代码:

 

 
  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 = "郑州,新乡,安阳,信阳"
  52. break
  53. return city; 

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. 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方法实现刷新 
  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=" 查询" 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> 

后台代码:

 

 
  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无刷新联动

该例子也要用到两个页面: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. 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方法实现刷新 
  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=" 查询" 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后台代码:

 

 
  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("郑州,新乡,安阳,信阳"); 
  28. break

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

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

图片精选