达也专栏

程序、老婆、钱、一个都不能少

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

须要用到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

}

阅读更多
个人分类: ASP.NET学习备注
上一篇如何在后台正确使用脚本
下一篇一些小技巧汇总
想对作者说点什么? 我来说一句

TextBox记录上次输入值

2013年11月28日 17KB 下载

没有更多推荐了,返回首页

关闭
关闭