功能:在textbox中输入内容,动态从数据库模糊查询显示到下拉框中,以供选择
  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](王朝网络 wangchao.net.cn)

Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中的更多相关文章

  1. JavaScript解决select下拉框中的内容太长显示不全的问题

    JavaScript解决select下拉框中的内容太长显示不全的问题 1.说明 有些情况下,select下拉框的内容过长,导致部分看不见: 现在通过鼠标事件,让下拉框中的内容显示完全 2.实现源码 & ...

  2. 利用js取到下拉框中选择的值

    现在的需求是:下拉框中要是选择加盟商让其继续选择学校,要是选择平台管理员则不需要选择学校.隐藏选择下拉列表. 选择枚举值: /// <summary> /// 平台角色 /// </ ...

  3. 让下拉框中同时显示Key与Value

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  4. jquery选中将select下拉框中一项后赋值给text文本框

    jquery选中将select下拉框中一项后赋值给text文本框,出现无法将第一个下拉框的value赋值给文本框 因为select默认选中第一项..在选择第一项时,便导致无法激发onchange事件. ...

  5. 快速解决js开发下拉框中blur与click冲突

    在开发中我们会经常遇到blur和click冲突的情况.下面叙述了开发中常遇到的"下拉框"的问题,并提供了两种解决方案. 一.blur和click事件简述 blur事件:当元素失去焦 ...

  6. JavaScript向select下拉框中加入和删除元素

    JavaScript向select下拉框中加入和删除元素 1.说明 a   利用append()方法向下拉框中加入元素 b   利用remove()方法移除下拉框中最后一个元素 2.设计源代码 < ...

  7. JavaScript向select下拉框中添加和删除元素

    JavaScript向select下拉框中添加和删除元素 1.说明 a   利用append()方法向下拉框中添加元素 b   利用remove()方法移除下拉框中最后一个元素 2.设计源码 < ...

  8. JavaScript获取select下拉框中的第一个值

    JavaScript获取select下拉框中的第一个值 1.说明 获取select下拉框中的第一个值 2.实现源码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  9. Java-Selenium,获取下拉框中的每个选项的值,并随机选择某个选项

    今天逛51testing,看见有人问这个问题.现在以Select标签为例. 1.首先看页面中的下拉框,如图: 2.F12查看页面源代码,如下 <select class="form-c ...

随机推荐

  1. extjs_09_定义自己的页面组件

    1.项目截图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWRhbV93enM=/font/5a6L5L2T/fontsize/400/fill/I0J ...

  2. opengl 扳回一球

    本文介绍了使用两种方法opengl画一个球体,一个是一个球形点位置计算,然后绘制出,还有一个glut套件自带功能. 一.直接绘制法 直接贴代码,解释都写在凝视里了.绘制时先移动好坐标系.然后调用这方法 ...

  3. adb这点小事——远程adb调试

    欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 1.   前言 1.1.  写在前面的话 在之前的一篇文章<360电视助手实现研究>中介绍了在局域网内直接 ...

  4. iptables的CLUSTER target以太网交换机和想法

    周末热风,这个想法从未在我的心脏像样的雨一阵悲哀. 每到周末,我会抽出一周整夜的事情的总结,无论是工作.人生,或者在上班或在锯的方式方法,并听取了抑制书评,因为无雨,周六晚上,我决定好好睡一觉,再折腾 ...

  5. 【Android】Android在AlertDialog使用大全

    package com.ceac.deng; import android.R.string; import android.support.v7.app.ActionBarActivity; imp ...

  6. Use PRODUCT_USER_PROFILE To Limit User

    The PRODUCT_USER_PROFILE (PUP) table provides product-level security that supplements the user-level ...

  7. TCO14 2C L2: CliqueGraph,graph theory, clique

    称号:http://community.topcoder.com/stat?c=problem_statement&pm=13251&rd=16017 參考:http://apps.t ...

  8. 照片详细解释YUV420数据格式

    YUV格式有两大类:planar和packed. 对于planar的YUV格式.先连续存储全部像素点的Y.紧接着存储全部像素点的U.随后是全部像素点的V. 对于packed的YUV格式,每一个像素点的 ...

  9. C#中简单的写日志的方法

    public static void WriteLog(string msg)        {            string appPath = AppDomain.CurrentDomain ...

  10. SpringMVC4 + Spring + MyBatis3

    SpringMVC4 + Spring + MyBatis3 本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我 ...