如何编写代码两个文本框,用于从数据库获取到gridview的自动完成搜索

时间:2013-06-04 19:54:18

标签: jquery asp.net ajax

iam使用两个文本框从数据库中自动完成搜索(1是用户名,2是类别,它是另一个表格)和获取数据到gridview,我只写一个texbox请帮帮我

    function ShowProcessImage() {
        var autocomplete = document.getElementById('txtSecSearch');
        autocomplete.style.backgroundImage = 'url(loading1.gif)';
        autocomplete.style.backgroundRepeat = 'no-repeat';
        autocomplete.style.backgroundPosition = 'right';
    }

    function HideProcessImage() {
        var autocomplete = document.getElementById('txtSecSearch');
        autocomplete.style.backgroundImage = 'none';
    }
    $(document).ready(function () {
        SearchText();
    });

    function SearchText() {
        $(".autosuggest").autocomplete

        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Default.aspx/GetAutoCompleteData",
                data: "{'ENAME':'" + document.getElementById('txtSNamesearch').value + "'}",
                dataType: "json",

                success: function (data) {
                    response(data.d);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }
    });
    }

1 个答案:

答案 0 :(得分:0)

尝试ajaxautocompleteextender你会得到它这是设计部分

                                                              
<div id='container'>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" GridLines="None" ForeColor="#333333">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnk_name" class='click' runat="server" Text='<%#Eval("empname") %>'>
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="dept" HeaderText="Dept" />
                </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

</div>

C#代码试试吧

但与jQuery autocomplate search

相比,它有点慢
protected void Page_Load(object sender, EventArgs e)
{

}

public static string GetTextBoxValue(System.Web.UI.Page FormPage, String ControlName)
{
    object FormControl = FormPage.FindControl(ControlName);

    if (FormControl == null)
        return "";

    return ((System.Web.UI.WebControls.TextBox)FormControl).Text;
}

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetCompletionList(string prefixText, int count, string contextKey)
{
    _Default dflt=new _Default();
    SqlConnection con = new SqlConnection("user id=sa;password=123;database=prince");
    con.Open();
    //using (SqlConnection con = new SqlConnection("database="))
    List<string> result = new List<string>();

    SqlCommand cmd = new SqlCommand("select empname from Emp where empname like '" + prefixText + "%'", con);

    SqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        result.Add(dr["empname"].ToString());

    }
    return result;
    con.Close();
}

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static List<string> GetCompletionList1(string prefixText, int count, string contextKey)
{
    _Default dflt = new _Default();
    SqlConnection con = new SqlConnection("user id=sa;password=123;database=prince");
    con.Open();
    //using (SqlConnection con = new SqlConnection("database="))
    List<string> result = new List<string>();

    SqlCommand cmd = new SqlCommand("select [DeptName] from [dbo].[Dept] where deptname like '" + prefixText + "%'", con);

    SqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        result.Add(dr["DeptName"].ToString());

    }
    return result;
    con.Close();
}



protected void txtEmpname_TextChanged(object sender, EventArgs e)
{

    SqlConnection con = new SqlConnection("user id=sa;password=123;database=prince");
    SqlDataAdapter cmd = new SqlDataAdapter("select empid,empname,empfname,dob,salary,dept,d.deptid from Emp e join Dept d on e.dept=d.deptname and e.empname='" + txtEmpname.Text + "'", con);
    DataSet ds = new DataSet();
    cmd.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    txtDeptName.Text = "";
    txtEmpname.Text = "";

}

protected void txtDeptName_TextChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("user id=sa;password=123;database=prince");
    SqlDataAdapter cmd = new SqlDataAdapter("select empid,empname,empfname,dob,salary,dept,d.deptid from Emp e join Dept d on e.dept=d.deptname and d.deptname='" + txtDeptName.Text + "'", con);
    DataSet ds = new DataSet();
    cmd.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    txtDeptName.Text = "";
    txtEmpname.Text = "";
}