如何用ajax在textbox模糊查询之后绑定数据到dropdownlist
如何用ajax在textbox模糊查询之后绑定数据到dropdownlist,在网上找了一个例子,单独运行可以,假如到项目里面就不行,真是奇怪。。。
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;
using System.Configuration;
public partial class xm_test : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(xm_test));
if (!Page.IsPostBack)
{
this.TextBox1.Attributes.Add("onchange", "cityResult();");
this.DropDownList1.Attributes.Add("onclick", "getData();");
}
}
#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()
{
}
#endregion
#region GetCityList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public DataSet GetCityList(string id)
{
string sql = "select * from yx_user where user_id like '%" + id + "%'";
return GetDataSet(sql);
}
#endregion
#region GetDataSet
public static DataSet GetDataSet(string sql)
{
string ConnectionString = ConfigurationManager.ConnectionStrings["partyConnectionString"].ToString();
SqlDataAdapter sda = new SqlDataAdapter(sql, ConnectionString);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xm_test.aspx.cs" Inherits="xm_test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function cityResult()
{
var city=document.getElementById("TextBox1");
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].user_name;
var id=ds.Tables[0].Rows[i].user_id;
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"></asp:DropDownList>
</form>
</body>
</html>
总是提示xm_test未定义。。。
[解决办法]
要在配置文件里面配置
<httpHandlers>
<add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
</httpHandlers>