簡體   English   中英

如何將ID從處理程序頁面發送到ASPX頁面

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM