如何实现跟随功能? [英] how to iplement follwing functunality?

查看:77
本文介绍了如何实现跟随功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,您可以在其中输入一些名称.我有一个数据库,其中包含可用名称的列表.现在如果有人在文本框中输入``a'',则应该下拉所有从``a''开始的名称;如果有人输入``b'',则整个文本将变为``ab'',因此应该下拉所有名称以"ab"开头.任何指针如何实现此功能?

I have a text box where you enter some name . i have a database which contains list of the names available . now if someone enters ''a'' in the text box it should dropdown all names starting from ''a''.If someone enters ''b'' now whole text will become ''ab'' so it should dropdown all names starting with ''ab'' .any pointers how to implement this functionality ?

推荐答案

您好,亲爱的,

实现此功能非常简单.当用户在文本框中输入任何名称时,在文本框中,文本更改事件将使用like运算符向数据库查询,该操作符将为您提供所有名称(以用户输入的字母开头).然后将数据绑定到下拉列表.查询将是

从<您的表格>中选择*其中的名称,例如"+""+ textbox.text +"%"+""
Hello dear,

It is very simple to achieve this functionality. When user enters any name in the textbox then in the textbox text changed event fire a query to the database using like operator which will give you all the names starting with the letter entered by the user. Then bind the data to the dropdownlist. The query will be

select * from <your table> where name like " + "''" + textbox.text + "%" + "''"


您需要使用ComboBox而不是TextBox.您并不需要更改组合框的内容.您只想基于当前键入的文本更改选择.每次键入字符都会更改绑定,这将导致过多的开销.
You need to use a ComboBox instead of a TextBox. Further, you don''t want to necessarily change the contents of the combobox. You merely want to change the selection based on the currently typed text. Changing the binding every time a character is typed is going to cause too much overhead.


此功能称为自动完成文本框
在asp.net 3.5中,它支持AjaxControlToolKit,它是开源的&可用于您的应用程序.
首先从以下位置下载AjaxControlToolKit.

http://ajaxcontroltoolkit.codeplex.com/ [ ^ ]

然后在ur asp.net bin文件夹中复制AjaxControlToolKit的dll.

&在ur asp.net页面顶部添加以下代码行.

This Functionality is called as AutoComplete TextBox
in asp.net 3.5 it supports AjaxControlToolKit which is open source & can be used in ur application.
First Download AjaxControlToolKit from Following Location.

http://ajaxcontroltoolkit.codeplex.com/[^]

Then in ur asp.net bin Folder Copy the dll of AjaxControlToolKit.

& add Following Line of code at top of ur asp.net page.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>



将ScriptManager添加到页面,这对于Ajax应用程序是必需的.



Add ScriptManager to page, Which required for Ajax Application.

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>



将一个WebService文件(.asmx)添加到您的项目中.

添加.asmx文件后,.cs文件将保存在App_code文件夹中.在.cs文件中添加此类型的方法.



Add One WebService file(.asmx) into ur project.

after adding .asmx file .cs file will saved in App_code Folder. add this type of method in .cs file.

[WebMethod(EnableSession = true)]
    public string[] ItemName(string prefixText,int count)
    {
        int branchId = (int)Session["BranchId"];
        using (var db = new WanaDCDataContext())
        {
            var items = new List<String>();
            JavaScriptSerializer serailizer = new JavaScriptSerializer();
            var list = (from it in db.ItemMasters where it.BranchId == branchId && (it.ItemName).ToLower().StartsWith(prefixText) orderby it.ItemName ascending select it).Take(count);
            foreach (ItemMaster c in list)
            {
                var html = c.ItemId;
                //var item = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(c.ItemName, c.ItemId);
                string item = AutoCompleteExtender.CreateAutoCompleteItem(c.ItemName, serailizer.Serialize(html));
                items.Add(item);
            }
            return items.ToArray();


        }
    }



这是返回字符串数组的Web服务方法.我已经使用linq To SQL类从数据库检索数据.


然后从asp.net页面调用此Web服务,如下所示.



This is Web Service Methods Which Returns String array. i have used linq To SQL Classes to retrieve data from Database.


Then call this web service from asp.net page as below.

<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" TargetControlID="txtStkItem"

                                        OnClientItemSelected="OnItemSelected" CompletionSetCount="60" ServiceMethod="ItemName"

                                        ServicePath="AjaxAutoCompleteService.asmx" MinimumPrefixLength="1" CompletionInterval="100"

                                        CompletionListCssClass="completionList" CompletionListItemCssClass="listItem"

                                        CompletionListHighlightedItemCssClass="itemHighlighted" runat="server">
                                    </cc1:AutoCompleteExtender>



确保您指定了正确的服务方法,服务路径.如有任何困难,请回复.

谢谢&问候,
沙爹.



Make Sure You specify correct service Method,Service Path. reply for any difficulty.

Thanks & Regards,
satya.


这篇关于如何实现跟随功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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