[英]How to send ID from handler page to aspx page
我正在asp.net上工作并陷入此问题。 我已使用处理程序应用了全文搜索功能。 现在,我希望当用户从列表中选择一个名称(输入关键字后建议)时,页面应重定向到该人的个人资料
<link href="Content/jquery.autocomplete.css" rel="stylesheet" />
<script src="Scripts/jquery-1.4.1.min.js"></script>
<script src="Scripts/jquery-1.3.2.min.js"></script>
<script src="Scripts/jquery.autocomplete.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#<%=txtSearch.ClientID%>").autocomplete('Search_CS.ashx');
});
</script>
<div>
<asp:TextBox ID="txtSearch" runat="server" ></asp:TextBox>
</div>
当用户在文本框中键入名称时,它将返回带有匹配搜索文本的用户名。 在处理程序中处理(ashx文件)
public void ProcessRequest (HttpContext context) {
string prefixText = context.Request.QueryString["q"];
using (SqlConnection conn = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Profile_ID,FirstName, LastName from UserProfile where FirstName like '%' + @SearchText + '%' OR LastName like '%' + @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", prefixText);
cmd.Connection = conn;
StringBuilder sb = new StringBuilder();
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sb.Append(sdr["FirstName"]).Append(" ").Append(sdr["LastName"]).Append(Environment.NewLine);
}
}
conn.Close();
context.Response.Write(sb.ToString());
}
}
}
现在,当用户键入“ Tom”时,将打开一个列表,其中所有用户的名称均为“ tom”。 当用户选择“ Tom John”时,页面应导航到Tom John个人资料。 这可以通过选择用户选择的用户名的Profile_ID来完成。 如何通过Profile_ID将用户重定向到特定用户的个人资料页面。
您可以实现在选择事件上使用示例
$(function() {
$("#<%=txtSearch.ClientID%>").autocomplete({
source: "Search_CS.ashx",
// you need to handle on select event
select: function( event, ui ) {
// here you can redirect ur user page
// http://jqueryui.com/autocomplete/#remote
window.top.location = this.value;
}
});
});
$(document).ready(function() {
$('#<%=txtSearch.ClientID%>').autocomplete({
minLength: 3,
source: "Search_CS.ashx",
select: function(event, ui) {
window.location.href = 'profile.aspx?user=' + ui.item.value;
}
});
});
有关更多详细信息,请参见文档 。
“源”属性代表数据源的名称,这是服务器端脚本,应返回JSON数据。 用户输入字符串(最小长度:3)后,插件将查询“ Search_CS.ashx?term = user_input”,并期望如下所示的JSON输出
[
{"id":"123","label":"John","value":"123"},
{"id":"435","label":"Bill","value":"435"}
]
因此,就您的代码而言,
string prefixText = context.Request.QueryString["term"];
....
sb.Append("[");
while (sdr.Read())
{
string u = sdr["FirstName"].ToString() + " " + sdr["LastName"].ToString();
if (sb.Length > 1)
sb.Append(",");
sb.AppendFormat("{{\"id\":\"{0}\",\"label\":\"{0}\",\"value\":\"{0}\"}}", u);
}
sb.Append("]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.