0

我使用两个文本框从数据库中自动完成搜索(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");
                }
            });
        }
    });
    }
4

1 回答 1

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 自动编译搜索相比,它有点慢


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 = "";
}
于 2013-06-13T18:02:11.367 回答