尝试 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 = "";
}