关闭

如何在TextBox输入时动态读取DataBase内容

902人阅读 评论(0) 收藏 举报

须要用到ajax

html代码
<html>
<head runat="server">
    <title>Untitled Page</title>
    <SCRIPT language="javascript">           
            //城市------------------------------
            function cityResult()
            {
                var city=document.getElementById("TextBox1");
                Page_Wonderful_Ajax_AjaxGetData.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" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:DropDownList ID="DropDownList1" runat="server" style="display:none">
        </asp:DropDownList></div>
    </form>
</body>
</html>

 

cs代码
public partial class Page_Wonderful_Ajax_AjaxGetData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Page_Wonderful_Ajax_AjaxGetData));
        if (!Page.IsPostBack)
        {
            this.TextBox1.Attributes.Add("onchange", "cityResult();");
            this.DropDownList1.Attributes.Add("onclick", "getData();");
        }
    }      

    #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

    #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

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22062次
    • 积分:309
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:14篇
    • 译文:0篇
    • 评论:3条
    文章存档
    最新评论