功能:在文字方塊中輸入內容,動態從資料庫模糊查詢顯示到下拉框,可供選擇
1.建立一個aspx頁面,html程式碼
<HTML>
<頭>
<標題>WebForm1</標題>
<腳本語言=“javascript”>
//城市-----------------------------
函數城市結果()
{
var city=document.getElementById("TextBox1");
WebForm1.GetCityList(city.value,get_city_Result_CallBack);
}
函數 get_city_Result_CallBack(響應)
{
if (response.value != null)
{
//偵錯工具;
document.getElementById("DropDownList1").style.display="block";
document.getElementById("DropDownList1").length=0;
var ds = 回應.值;
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));
}
}
}
別的
{
document.getElementById("DropDownList1").style.display="none";
}
返回
}
函數 getData()
{
var Province=document.getElementById("DropDownList1");
var pindex = 省.selectedIndex;
var pValue = Province.options[pindex].value;
var pText = Province.options[pindex].text;
document.getElementById("<%=TextBox1.ClientID%>").innerText=pText;
}
</腳本>
</頭>
<正文>
<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>
</形式>
</正文>
</HTML>2.cs程式碼
使用系統;
使用系統集合;
使用 System.ComponentModel;
使用系統數據;
使用系統繪圖;
使用系統.Web;
使用 System.Web.SessionState;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.HtmlControls;
使用 System.Data.SqlClient;
名稱空間 ajaxselect
{
/**//// <摘要>
/// WebForm1 的摘要描述。
/// </摘要>
公用類別WebForm1:System.Web.UI.Page
{
受保護的 System.Web.UI.WebControls.TextBox TextBox1;
受保護的 System.Web.UI.WebControls.DropDownList DropDownList1;
私人無效Page_Load(物件發送者,System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(WebForm1));
if (!Page.IsPostBack)
{
this.TextBox1.Attributes.Add("onchange", "cityResult();");
this.DropDownList1.Attributes.Add("onclick", "getData();");
}
Web 表單設計器產生的程式碼#region Web 表單
設計
器產生的程式碼
覆蓋 protected void OnInit(EventArgs e)
{
//
// CODEGEN:ASP.NET Web 窗體設計器需要此呼叫。
//
初始化組件();
基.OnInit(e);
}
/**//// <摘要>
/// 設計器支援所需的方法 - 請勿修改
/// 使用程式碼編輯器編輯此方法的內容。
/// </摘要>
私有無效InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
取得城市列表#region 取得城市列表
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
公共資料集GetCityList(int省份ID)
{
string sql = "從父親喜歡'%'的城市中選出*" + Provinceid + "%'";
返回 GetDataSet(sql);
}
#endregion
取得資料集#region 取得資料集
公共靜態資料集GetDataSet(字串sql)
{
字串 ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
SqlDataAdapter sda = new SqlDataAdapter(sql, ConnectionString);
資料集 ds = new 資料集();
sda.Fill(ds);
返回ds;
}
#endregion
}
}3.來源程式碼下載
4.資料庫腳本
創建表 [dbo].[城市](
[id] [int] NOT NULL,
[cityID] [nvarchar](6) COLLATE Chinese_PRC_CI_AS NULL,
[城市] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[父親] [nvarchar](6) COLLATE 中文_PRC_CI_AS NULL,
約束 [PK_city] 主鍵聚集
(
[id]ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [主要]
) 關於 [主要]