实现无刷新联动例子汇总(2)

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

前台代码:

<%@ 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 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 runat="server"> <div> <table cellpadding="5" cellspacing="0"> <tr> <td colspan="2"> callback方法实现刷新 </td> </tr> <tr> <td> 选择省份: </td> <td> <select> <option value="湖北">湖北</option> <option value="河北">河北</option> <option value="广东">广东</option> <option value="河南">河南</option> </select> <input type="button" value=" 查询" /> </td> </tr> <tr> <td> 城市: </td> <td> <asp:DropDownList runat="server"> </asp:DropDownList> </td> </tr> </table> </div> </form> </body> </html>

后台代码:

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页面前台代码:

<%@ 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 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 runat="server"> <div> <table cellpadding="5" cellspacing="0"> <tr> <td colspan="2"> ajax方法实现刷新 </td> </tr> <tr> <td> 选择省份: </td> <td> <select> <option value="湖北">湖北</option> <option value="河北">河北</option> <option value="广东">广东</option> <option value="河南">河南</option> </select> <input type="button" value=" 查询" /> </td> </tr> <tr> <td> 城市: </td> <td> <asp:DropDownList runat="server"> </asp:DropDownList> </td> </tr> </table> </div> </form> </body> </html>

datapage.aspx后台代码:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgjwws.html