简体   繁体   English

如何将ID从处理程序页面发送到ASPX页面

[英]How to send ID from handler page to aspx page

I am working on asp.net and stuck in this problem. 我正在asp.net上工作并陷入此问题。 I have applied full text search functionality using handler. 我已使用处理程序应用了全文搜索功能。 And now I want that when user chooses a name from list (which is suggested after entering keywords) page should be redirected to that person's profile 现在,我希望当用户从列表中选择一个名称(输入关键字后建议)时,页面应重定向到该人的个人资料

    <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>

When a user types a name in textbox, it returns usernames with matching search text. 当用户在文本框中键入名称时,它将返回带有匹配搜索文本的用户名。 It is handling in handler(ashx file) 在处理程序中处理(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());

    }
}
   }

Now when user types "Tom" a list is opened with all the users having name "tom". 现在,当用户键入“ Tom”时,将打开一个列表,其中所有用户的名称均为“ tom”。 When user selects "Tom John", page should navigate to Tom John profile. 当用户选择“ Tom John”时,页面应导航到Tom John个人资料。 And this can be done by selecting Profile_ID of the username which is selected by the user. 这可以通过选择用户选择的用户名的Profile_ID来完成。 How to redirect user to particular user's profile page through Profile_ID. 如何通过Profile_ID将用户重定向到特定用户的个人资料页面。

you can achive that using on select event here is example 您可以实现在选择事件上使用示例

$(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;
        }
    });
});

See documentation for more details. 有关更多详细信息,请参见文档

The "source" attribute stands for the name of the datasource, a server-side script which should return JSON data. “源”属性代表数据源的名称,这是服务器端脚本,应返回JSON数据。 Once user enters a string (minLength: 3) the plugin queries "Search_CS.ashx?term=user_input", and expects a JSON output as below 用户输入字符串(最小长度:3)后,插件将查询“ Search_CS.ashx?term = user_input”,并期望如下所示的JSON输出

[
   {"id":"123","label":"John","value":"123"},
   {"id":"435","label":"Bill","value":"435"}
]

So, in terms of your code it should look as 因此,就您的代码而言,

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM