Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中 - 站长学院未来的中国站长, 更加专注中国网络的发展 广告服务 | 最新更新 |
加入收藏 | 设为首页
首页
互联网软件
IDC产品
站长交易
项目外包
商业信息
招聘求职
公司库
资 讯
下 载
学 院
工 具
论 坛
热门关键字:CSS布局配色参考Dreamweaver8 apacheApacheIIS6AjaxWML
推
荐
信
息 九网互联:不稳定无条件退款
阳光互联:代理国际域名 超低价36元
.com国际域名注册35元,主机3折起
免费提供DZ等论坛空间
1G美国全能空间【199元/年】免备案
3G全能虚拟主机100元/年
星辉互联全能空间1G 150元/年 送域名
快速提升Alexa+来路流量IP
代理ICP,SP,网络文化许可证
商务移动办公首选协智企业邮箱
59互联服务器 COM43元 CN18元
COM域名39元,1G全能空间99元
首页 | HOME
站长之家
站长入门
运营策划
搜索优化
网站盈利
网页设计
Html
CSS/Xhtml
Dhtml
WAP
Dreamweaver
设计原理
平面动画
Photoshop
Firework
Flash
网站开发
Asp
.Net
Php
Jsp
Mssql
Access
Oracle
Mysql
服务器
Fressbsd
LINUX
Windows
视频教程常用手册常用广告代码js特效代码
站长学院 >> 网页设计教程 >> Dhtml教程 >> Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中
Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中
2008-4-14 1:30:00查看学习心得
功能:在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]
本文作者:
页面收藏到: [Baidu收藏] [收藏到QQ书签] [新浪ViVi] [Poco] [bbmao] [365KEY] [天极网摘] [我摘]
点击复制本页地址,传给QQ/MSN上的好友
上一篇下一篇
更多»学习心得
笔名 匿名
Email
最新教程
JavaScript 异步调用框架 (Part 5 - 链式实现)07-14
忙里偷闲写的jquery的屏遮层插件07-14
JavaScript教程:伸缩菜单的制作07-14
拆分自然数:纯while实现 (Part 1 - 思路)07-14
javascript通过按钮实现超链接07-14
基于JavaScript的REST客户端框架07-14
JS操作Cookie 大全07-14
Javascript命名规范07-14
学习Jquery之旅06-15
javascript API架子06-15
热门教程
JavaScript更改class和id的方法01-02
JAVASCRIPT中的window.location和document.loc