【解決方法】asp.net、C#のテキストボックスを使用してAJaxを介して検索する


先生、サンディヤです。

どなたでも、AJAX を使用してデータを検索するためのコードとデザイン ページを送ってください。

Googleのように検索する必要があります。

ちなみに、gridviewからデータを検索する必要があります。 1 文字を入力すると、その詳細がすべて表示されるはずです。 Google 検索と同じように。 テキストボックス内にもウォーターマークが必要です。

どなたかデザインとコードページを送ってください。

本当にありがとうございます、先生、

解決策 2

XML
[WebMethod]

public static List<string> GetAutoCompleteData(string username)
{
List<string> result = new List<string>();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
using (SqlCommand cmd = new SqlCommand("select DISTINCT UserName from UserInformation where UserName LIKE '%'+@SearchText+'%'", con))
{
con.Open();
cmd.Parameters.AddWithValue("@SearchText", username);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
result.Add(dr["UserName"].ToString());
}
return result;
}

解決策 1

このリンクを参照してください
http://www.asp.net/ajaxlibrary/act_AutoComplete_Simple.ashx

このためには WebService を作成する必要があります。

解決策 4

この解決策は WebService、Entity Framework、Jquery、JqueryUI を使用します

ページを見る

HTML
<html lang="en">
<head>
    <title>jQuery UI Autocomplete</title>
    
    <link rel="stylesheet" 
    href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")">

    <script src="@Url.Content("~/Scripts/jquery-1.5.2.min.js")" 
     type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.16.min.js")" 
     type="text/javascript"></script>
    

	<script
	    $(function () {
	        $("#txtSiteName").autocomplete({
	            source: function (request, response) {
	                $.ajax({
	                    type: "POST",
	                    contentType: "application/json; charset=utf-8",
	                    url: "/WebService1.asmx/GetAllSites",
	                    data: "{'keywords':'" + request.term + "'}",
	                    dataType: "json",
	                    async: true,
	                    success: function (data) {
	                        response(data.d);
	                    },
	                    error: function (result) {
	                        //alert("Error");
	                    }
	                });
	            }
	        });
	    });
	</script>
</head>
<body>
    <!-- When you type in textBox it will AutoComplete-->
    <input id="txtSiteName" type="text" /> 

</body>
</html>

ウェブサービス

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script,
    // using ASP.NET AJAX, uncomment the following line.
    [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        private Model1Container context = new Model1Container();

        [WebMethod]
        public IList<string> GetAllSites(string keywords)
        {
            IList<string> result;

            result = context.Entity1
                        .Where(x => x.Sites.Contains(keywords))
                        .Select(y => y.Sites).ToArray();

            try
            {                return result;          }
            catch
            {                return null;            }
        }
    }
}

解決策 5

C# ______
[System.Web.Script.Services.ScriptMethod]
[System.Web.Services.WebMethod]

public static List getCustomerNames(string prefixText)
{
List 顧客 = new List();

SqlConnection conn = new SqlConnection(@”データ ソース=RRSOFTSERVER\RRSOFTIT; 初期カタログ=demo; ユーザー ID=sa;パスワード=rrit@1234″);

using (SqlCommand cmd = new SqlCommand())
{

cmd.CommandText = “P_name が @Text + ‘%’ のような jsonproduct から P_name を選択します”;
cmd.Parameters.AddWithValue(“@Text”, prefixText);
cmd.Connection = 接続;
conn.Open();
(SqlDataReader dr = cmd.ExecuteReader()) を使用して
{
while (dr.Read())
{
顧客.Add(dr[“P_name”].ToString());
}
}

}
conn.Close();
再来客。

}

aspx..コード

<%@ 登録アセンブリ="AjaxControlToolkit" 名前空間="AjaxControlToolkit" TagPrefix="cc1" %>


注: AjaxTOOLKIT.Dll をダウンロードします。
ビンフォルダーの追加

コメント

タイトルとURLをコピーしました