Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中

功能:在textbox中输入内容,动态从数据库模糊查询显示到下拉框中,以供选择

1.建立一aspx页面,html代码

None.gif < HTML >
None.gif    
< HEAD >
None.gif        
< title > WebForm1 </ title >
ExpandedBlockStart.gifContractedBlock.gif        
< SCRIPT  language ="javascript" > dot.gif             
InBlock.gif            
//城市------------------------------
InBlock.gif
            function cityResult() 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
var city=document.getElementById("TextBox1");
InBlock.gif                WebForm1.GetCityList(city.value,get_city_Result_CallBack);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
function get_city_Result_CallBack(response)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (response.value != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    
//debugger;
InBlock.gif
                    document.getElementById("DropDownList1").style.display="block";
InBlock.gif                    document.getElementById(
"DropDownList1").length=0;                
InBlock.gif                
var ds = response.value;
InBlock.gif                    
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                    
InBlock.gif                        
for(var i=0; i<ds.Tables[0].Rows.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
var name=ds.Tables[0].Rows[i].city;
InBlock.gif                      
var id=ds.Tables[0].Rows[i].cityID;
InBlock.gif                      document.getElementById(
"DropDownList1").options.add(new Option(name,id));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    document.getElementById(
"DropDownList1").style.display="none";
ExpandedSubBlockEnd.gif                }
             
InBlock.gif                
return
ExpandedSubBlockEnd.gif            }

InBlock.gif           
InBlock.gif            
function getData()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var province=document.getElementById("DropDownList1");
InBlock.gif                
var pindex = province.selectedIndex;
InBlock.gif                
var pValue = province.options[pindex].value;
InBlock.gif                
var pText  = province.options[pindex].text;                                                
InBlock.gif
InBlock.gif                document.getElementById(
"<%=TextBox1.ClientID%>").innerText=pText;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        
</ SCRIPT >
None.gif    
</ HEAD >
None.gif    
< body >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< asp:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >
None.gif            
< br >
None.gif            
< asp:DropDownList  ID ="DropDownList1"  runat ="server"  Width ="192px"  style ="display:none" ></ asp:DropDownList >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
2.cs代码
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  ajaxselect
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for WebForm1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Ajax.Utility.RegisterTypeForAjax(
typeof(WebForm1));
InBlock.gif            
if (!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.TextBox1.Attributes.Add("onchange""cityResult();");
InBlock.gif                
this.DropDownList1.Attributes.Add("onclick""getData();");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetCityList#region GetCityList
InBlock.gif        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
InBlock.gif        
public DataSet GetCityList(int provinceid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql = "select * from city where father like '%" + provinceid + "%'";
InBlock.gif            
return GetDataSet(sql);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataSet#region GetDataSet
InBlock.gif        
public static DataSet GetDataSet(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
InBlock.gif            SqlDataAdapter sda 
= new SqlDataAdapter(sql, ConnectionString);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            sda.Fill(ds);
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
3. 源代码下载    /Files/ipusr/ajaxselect.rar
4.数据库脚本
None.gif CREATE   TABLE   [ dbo ] . [ city ] (
None.gif    
[ id ]   [ int ]   NOT   NULL ,
None.gif    
[ cityID ]   [ nvarchar ] ( 6 ) COLLATE Chinese_PRC_CI_AS  NULL ,
None.gif    
[ city ]   [ nvarchar ] ( 50 ) COLLATE Chinese_PRC_CI_AS  NULL ,
None.gif    
[ father ]   [ nvarchar ] ( 6 ) COLLATE Chinese_PRC_CI_AS  NULL ,
None.gif 
CONSTRAINT   [ PK_city ]   PRIMARY   KEY   CLUSTERED  
None.gif(
None.gif    
[ id ]   ASC
None.gif)
WITH  (IGNORE_DUP_KEY  =   OFF ON   [ PRIMARY ]
None.gif
ON   [ PRIMARY ]

转载于:https://www.cnblogs.com/ipusr/archive/2007/03/03/662391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值