Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中
点击次数:41 次 发布日期:2008-11-26 10:48:02 作者:源代码网
|
源代码网推荐 源代码网推荐 1.建立一aspx页面,html代码 源代码网推荐 源代码网推荐 <HTML> 源代码网推荐 <HEAD> 源代码网推荐 <title>WebForm1</title> 源代码网推荐 <SCRIPT language="javascript"> 源代码网推荐 //城市------------------------------ 源代码网推荐 function cityResult() 源代码网推荐 { 源代码网推荐 var city=document.getElementById("TextBox1"); 源代码网推荐 WebForm1.GetCityList(city.value,get_city_Result_CallBack); 源代码网推荐 } 源代码网推荐 源代码网推荐 function get_city_Result_CallBack(response) 源代码网推荐 { 源代码网推荐 if (response.value != null) 源代码网推荐 { 源代码网推荐 //debugger; 源代码网推荐 document.getElementById("DropDownList1").style.display="block"; 源代码网推荐 document.getElementById("DropDownList1").length=0; 源代码网推荐 var ds = response.value; 源代码网推荐 if(ds != null && typeof(ds) == "object" && ds.Tables != null) 源代码网推荐 { 源代码网推荐 for(var i=0; i<ds.Tables[0].Rows.length; i++) 源代码网推荐 { 源代码网推荐 var name=ds.Tables[0].Rows[i].city; 源代码网推荐 var id=ds.Tables[0].Rows[i].cityID; 源代码网推荐 document.getElementById("DropDownList1").options.add(new Option(name,id)); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 document.getElementById("DropDownList1").style.display="none"; 源代码网推荐 } 源代码网推荐 return 源代码网推荐 } 源代码网推荐 源代码网推荐 function getData() 源代码网推荐 { 源代码网推荐 var province=document.getElementById("DropDownList1"); 源代码网推荐 var pindex = province.selectedIndex; 源代码网推荐 var pValue = province.options[pindex].value; 源代码网推荐 var pText = province.options[pindex].text; 源代码网推荐 源代码网推荐 document.getElementById("<%=TextBox1.ClientID%>").innerText=pText; 源代码网推荐 } 源代码网推荐 </SCRIPT> 源代码网推荐 </HEAD> 源代码网推荐 <body> 源代码网推荐 <form id="Form1" method="post" runat="server"> 源代码网推荐 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 源代码网推荐 <br> 源代码网推荐 <asp:DropDownList ID="DropDownList1" runat="server" Width="192px" style="display:none"></asp:DropDownList> 源代码网推荐 </form> 源代码网推荐 </body> 源代码网推荐 </HTML>2.cs代码 源代码网推荐 using System; 源代码网推荐 using System.Collections; 源代码网推荐 using System.ComponentModel; 源代码网推荐 using System.Data; 源代码网推荐 using System.Drawing; 源代码网推荐 using System.Web; 源代码网推荐 using System.Web.SessionState; 源代码网推荐 using System.Web.UI; 源代码网推荐 using System.Web.UI.WebControls; 源代码网推荐 using System.Web.UI.HtmlControls; 源代码网推荐 using System.Data.SqlClient; 源代码网推荐 namespace ajaxselect 源代码网推荐 { 源代码网推荐 /**//// <summary> 源代码网推荐 /// Summary description for WebForm1. 源代码网推荐 /// </summary> 源代码网推荐 public class WebForm1 : System.Web.UI.Page 源代码网推荐 { 源代码网推荐 protected System.Web.UI.WebControls.TextBox TextBox1; 源代码网推荐 protected System.Web.UI.WebControls.DropDownList DropDownList1; 源代码网推荐 源代码网推荐 private void Page_Load(object sender, System.EventArgs e) 源代码网推荐 { 源代码网推荐 Ajax.Utility.RegisterTypeForAjax(typeof(WebForm1)); 源代码网推荐 if (!Page.IsPostBack) 源代码网推荐 { 源代码网推荐 this.TextBox1.Attributes.Add("onchange", "cityResult();"); 源代码网推荐 this.DropDownList1.Attributes.Add("onclick", "getData();"); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 Web Form Designer generated code#region Web Form Designer generated code 源代码网推荐 override protected void OnInit(EventArgs e) 源代码网推荐 { 源代码网推荐 // 源代码网推荐 // CODEGEN: This call is required by the ASP.NET Web Form Designer. 源代码网推荐 // 源代码网推荐 InitializeComponent(); 源代码网推荐 base.OnInit(e); 源代码网推荐 } 源代码网推荐 源代码网推荐 /**//// <summary> 源代码网推荐 /// Required method for Designer support - do not modify 源代码网推荐 /// the contents of this method with the code editor. 源代码网推荐 /// </summary> 源代码网推荐 private void InitializeComponent() 源代码网推荐 { 源代码网推荐 this.Load += new System.EventHandler(this.Page_Load); 源代码网推荐 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐 源代码网推荐 GetCityList#region GetCityList 源代码网推荐 [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)] 源代码网推荐 public DataSet GetCityList(int provinceid) 源代码网推荐 { 源代码网推荐 string sql = "select * from city where father like "%" + provinceid + "%""; 源代码网推荐 return GetDataSet(sql); 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐 GetDataSet#region GetDataSet 源代码网推荐 public static DataSet GetDataSet(string sql) 源代码网推荐 { 源代码网推荐 string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]; 源代码网推荐 SqlDataAdapter sda = new SqlDataAdapter(sql, ConnectionString); 源代码网推荐 DataSet ds = new DataSet(); 源代码网推荐 sda.Fill(ds); 源代码网推荐 return ds; 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐 源代码网推荐 } 源代码网推荐 }3.源代码下载 源代码网推荐 4.数据库脚本 源代码网推荐 CREATE TABLE [dbo].[city]( 源代码网推荐 [id] [int] NOT NULL, 源代码网推荐 [cityID] [nvarchar](6) COLLATE Chinese_PRC_CI_AS NULL, 源代码网推荐 [city] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL, 源代码网推荐 [father] [nvarchar](6) COLLATE Chinese_PRC_CI_AS NULL, 源代码网推荐 CONSTRAINT [PK_city] PRIMARY KEY CLUSTERED 源代码网推荐 ( 源代码网推荐 [id] ASC 源代码网推荐 )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 源代码网推荐 ) ON [PRIMARY] 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
