I'm using jQuery AutoComplete, and I have an auto-complete search box to search for users.
Default.aspx
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#<%= txtUser.ClientID %>").autocomplete('UsersAutoComplete.ashx');
});
</script>
<asp:TextBox ID="txtUser" runat="server" />
UsersAutoComplete.ashx
public class UsersAutoComplete : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
List<User> users = DataContext.Users
.Where(u => u.Username.StartsWith(context.Request.QueryString["q"]))
.Take(Int32.Parse(context.Request.QueryString["limit"]))
.OrderBy(u => u.Username)
.ToList();
foreach (User user in users)
{
context.Response.Write(user.Username + Environment.NewLine);
}
}
public bool IsReusable
{
get { return false; }
}
}
As you can see, it only returns the Username. I'd also like to return the user's UserID as well.
The results will still only show a list of Usernames, but when someone selects a user, I'd like to store the UserID for that user in a hidden value so that I can do whatever I need to with it. I'll be running a query on the selected user, and I'd rather lookup the user by UserID than by Username.
Is there a way to do this?