如何在asp.net中执行AutoComplete文本框 [英] How to do AutoComplete textbox in asp.net

查看:81
本文介绍了如何在asp.net中执行AutoComplete文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi EveryBody,



在我的应用程序中,我想使用自动完成使用AJAX工具包将City数据绑定到文本框。我在页面中添加了以下源代码





 <   asp:TextBox     ID   =  TextBox1   < span class =code-attribute> runat   =  server >  <   / asp:TextBox  >  
< ajax:AutoCompleteExtender < span class =code-attribute> ID = AutoCompleteExtender1 runat = server TargetControlID = TextBox1

< span class =code-attribute> MinimumPrefixLength = 1

启用缓存 = true

CompletionInterval = 100

< span class =code-attribute> ServiceMethod = GetSuggessions

< span class =code-attribute> CompletionSetCount = 20

Contex tKey = strOtherPram >
< / ajax:AutoCompleteExtender >



以下是我的代码,其中我是在大学班级中存储的列表中的城市和商店

< pre lang =xml> public static List < tblCity > GetCities(long StateId)
{

List < tblCity > result = null;
using(var context = new HealthEntities())
{
result = context.tblCities
.Where(c => c.StateId == StateId)
.ToList < tblCity > ( );

}

返回结果;
}



我无法将数据绑定到文本框

解决方案

请参阅以下网站,



http://www.asp.net/ajaxlibrary/ act_AutoComplete_Simple.ashx [ ^ ]

您好,

您可以通过服务调用方法。在您的html中添加ServicePath。创建服务并调用您的方法。在我的代码中,该方法通过Stored Proc获取数据,您可以消除它。





 < span class =code-keyword><   cc1:AutoCompleteExtender     ID   =  AutoCompleteExtender2    runat   =  server     CompletionInterval   = < span class =code-keyword> 1000  

< span class =code-attribute> CompletionSetCount = 12 EnableCaching = true MinimumPrefixLength = 1 ServiceMethod = FindFileNumberMT

< span class =code-attribute> 已启用 = true ServicePath = ../ AutoFileNew.asmx TargetControlID = txtFileNumber >
< / cc1:AutoCompleteExtender >









使用System; 
使用System.Collections;
使用System.Collections.Generic;
使用System.Data;
使用System.Diagnostics;
使用System.Web;使用System.Web.Services
;
使用System.Web.Services.Protocols;
使用System.Data.SqlClient;
使用System.Data.Common;

[WebService(Namespace =http://tempuri.org/)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script。 Services.ScriptService()]
public class AutoFileNew:System.Web.Services.WebService
{

[WebMethod()]
public string [] FindFileNumberMT(string prefixText,int count)
{

List< string> titleArList = new List< string>();
SqlDataReader dr = null;

SqlCommand oCmd = new SqlCommand(sp_GetAutoFileNumbersNew,Myapplication.mConn);
oCmd.CommandType = CommandType.StoredProcedure;
oCmd.Parameters.Add(@ filenumber,SqlDbType.VarChar,50).Value = prefixText;

try {
dr = oCmd.ExecuteReader();
while(dr.Read()){
titleArList.Add(dr [file_number]。ToString());
}
返回titleArList.ToArray();

} catch(Exception ex){
} finally {
Myapplication.mConn.Close();
}
}

}


Hi EveryBody,

In My Application i want to bind City data to textbox using auto complete Using AJAX toolkit. I am added below source code in my page


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <ajax:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="TextBox1"

                MinimumPrefixLength="1"

                EnableCaching="true"

                CompletionInterval="100"

                ServiceMethod="GetSuggessions"

                CompletionSetCount="20"

                ContextKey="strOtherPram">
                </ajax:AutoCompleteExtender>


below is my code where i am retrive city and store in list which is store in Master Class

public static List<tblCity> GetCities(long StateId)
      {

          List<tblCity> result = null;
          using (var context = new HealthEntities())
          {
              result = context.tblCities
                  .Where(c => c.StateId == StateId)
                  .ToList<tblCity>();

          }

          return result;
      }


I am not able to bind data to textbox

解决方案

Refer following site,

http://www.asp.net/ajaxlibrary/act_AutoComplete_Simple.ashx[^]


Hi,
You can call a method thro a service. Add ServicePath in your html. Create a service and call your method. In my code, the method gets data via Stored Proc, which you can eliminate.


<cc1:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" CompletionInterval="1000"

                       CompletionSetCount="12" EnableCaching="true" MinimumPrefixLength="1" ServiceMethod="FindFileNumberMT"

                       Enabled="true" ServicePath="../AutoFileNew.asmx" TargetControlID="txtFileNumber">
                   </cc1:AutoCompleteExtender>





using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data.Common;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]
public class AutoFileNew : System.Web.Services.WebService
{

	[WebMethod()]
	public string[] FindFileNumberMT(string prefixText, int count)
	{

		List<string> titleArList = new List<string>();
		SqlDataReader dr = null;

		SqlCommand oCmd = new SqlCommand("sp_GetAutoFileNumbersNew",Myapplication.mConn);
		oCmd.CommandType = CommandType.StoredProcedure;
		oCmd.Parameters.Add("@filenumber", SqlDbType.VarChar, 50).Value = prefixText;

		try {
			dr = oCmd.ExecuteReader();
			while (dr.Read()) {
				titleArList.Add(dr["file_number"].ToString());
			}
			return titleArList.ToArray();

		} catch (Exception ex) {
		} finally {
			Myapplication.mConn.Close();
		}
	}

}


这篇关于如何在asp.net中执行AutoComplete文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆