推荐一款自动建议功能的AJAX文本框

AjaxLookup

利用Ajax.Net的dll和WebUi的TextBox做成的一款,支持自动建议功能的AJAX文本框控件。

使用方式极其简单,后台完成AJAX的建议提示的方法,前台添加进控件,稍作属性设置即可。
同一页面可以放置多个此控件。

后台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Ajax;
using ThirdParty.Net;

namespace axLookupDemo
{
    
/// <summary>
    
/// Summary description for _Default.
    
/// </summary>

    public class axlDemo : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label Label1;
        
protected AjaxLookup axLookup1;
        
protected ThirdParty.Net.AjaxLookup axLookup2;
        
protected System.Web.UI.WebControls.Label Label3;
        
protected ThirdParty.Net.AjaxLookup axLookup3;
        
protected System.Web.UI.WebControls.Label Label2;
    
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
//Ajax.Utility.RegisterTypeForAjax(GetType(ReferralsDB))
            Ajax.Utility.RegisterTypeForAjax(typeof(axlDemo));
        }


        [AjaxMethod()]
        
public ArrayList GetSearchItems(string query)
        
{
            ArrayList matchItems 
= new ArrayList();
            
string[] item1 = {"C1""Item 1 - note that the box will grow to the size of the largest line"};
            
string[] item2 = {"C2""Item 2 - unless a DivWidth is specified"};
            matchItems.Add(item1);
            matchItems.Add(item2);

            
return matchItems;
        }


        [AjaxMethod()]
        
public ArrayList GetSearchItemsCN(string query)
        
{
            query 
=  System.Web.HttpUtility.UrlDecode(query);
            
            
//测试如果是数字,按数字匹配
            
            
if ( IsValidInt( query ) )
            
{
                ArrayList items 
= GetRecordsCNByDM();
                ArrayList matchItems 
= new ArrayList();

                
if ( query.Length > 0 )
                
{
                
                    
foreach (string[] item in items)
                    
{
                        
if (item[0].StartsWith(query))
                        
{
                            matchItems.Add(item);
                        }


                    }

                }


                
return matchItems;
            }

            
else //测试非数字按拼音缩写或汉字查找
            {
                
if ( IsValidEnString( query) ) //拼音缩写
                {
                    ArrayList items 
= GetRecordsCNByPY();
                    ArrayList matchItems 
= new ArrayList();

                    
if ( query.Length > 0 )
                    
{
                
                        
foreach (string[] item in items)
                        
{
                            
if (item[0].ToUpper().StartsWith(query.ToUpper()))
                            
{
                                
string[] itemOut = new string[2];
                                itemOut[
0= item[1];
                                itemOut[
1= item[2];

                                matchItems.Add(itemOut);
                            }


                        }

                    }


                    
return matchItems;
                }

                
else //全词查找
                {
                    ArrayList items 
= GetRecordsCNByDM();
                    ArrayList matchItems 
= new ArrayList();

                    
if ( query.Length > 0 )
                    
{
                
                        
foreach (string[] item in items)
                        
{
                            
if (item[1].StartsWith(query))
                            
{
                                matchItems.Add(item);
                            }


                        }

                    }


                    
return matchItems;
                }

            }
            
        }


        
private ArrayList GetRecordsCNByDM()
        
{
            ArrayList items 
= new ArrayList();

            
string[,] item = new string[,] {
                                                
{"600616""G食品"},
                                                
{"600123""G瓜瓜"},
                                                
{"600601""G西西"},
                                                
{"600602""G西瓜"},
                                                
{"600102""G葡萄"},
                                                
{"600103""G枣子"},
                                                
{"600104""G哈密瓜"},
            }
;

            
for ( int i = 0; i < item.GetLength(0); i ++ )
            
{
                
string[] itemLine = new string[item.GetLength(1)];
                
                
for ( int j = 0; j < itemLine.Length; j ++ )
                
{
                    itemLine[j] 
= item[i,j].ToString();
                }


                items.Add( itemLine );
            }


            
return items;
        }


        
private ArrayList GetRecordsCNByPY()
        
{
            ArrayList items 
= new ArrayList();

            
string[,] item = new string[,] {
                                                
{"GSP""600616""G食品"},
                                                
{"GGG""600123""G瓜瓜"},
                                                
{"GXX""600601""G西西"},
                                                
{"GXG""600602""G西瓜"},
                                                
{"GPT""600102""G葡萄"},
                                                
{"GZZ""600103""G枣子"},
                                                
{"GHM""600104""G哈密瓜"},
            }
;

            
for ( int i = 0; i < item.GetLength(0); i ++ )
            
{
                
string[] itemLine = new string[item.GetLength(1)];
                
                
for ( int j = 0; j < itemLine.Length; j ++ )
                
{
                    itemLine[j] 
= item[i,j].ToString();
                }


                items.Add( itemLine );
            }


            
return items;
        }


        
///判断英文名 
        private bool IsValidEnString( string EnString )
        
{
            
return Regex.IsMatch( EnString, @"^[a-zA-Z]{1,30}$" ); 
        }


        
//判断整型
        private bool IsValidInt( string IntString )
        
{
            
return Regex.IsMatch( IntString, @"^[0-9]{1,6}$" ); 
        }


        
Web Form Designer generated code
    }

}


前台:就更简单了
<%@ Register TagPrefix="Ajax" Namespace="ThirdParty.Net" Assembly="AjaxLookup" %>
<%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="axLookupDemo.axlDemo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>Default</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="C#" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        
<script language="javascript" src="browser_detection.js"></script>
        
<script language="javascript" src="axlookup.js"></script>
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<asp:label id="Label1" style="Z-INDEX: 100; LEFT: 48px; POSITION: absolute; TOP: 264px" runat="server"
                Width
="80px" Visible="False">Control #1</asp:label>
            
<Ajax:AjaxLookup id="axLookup3" style="Z-INDEX: 106; LEFT: 40px; POSITION: absolute; TOP: 48px" Width="184px"
                CallBackFunction
="axlDemo.GetSearchItemsCN" HighlightColor="#FFF791" DivFont="Arial" DivPadding="1px"
                DivBorder
="1px solid gray" BackgroundColor="#EEE" FontSize="11px" FontWeight="bold" ItemStyleBorderBottom="0px"
                ItemStylePadding
="1px 0px 1px 0px" ItemStyleSpacing="10px" Runat="server" DivWidth="184px"></Ajax:AjaxLookup>
            
<asp:label id="Label3" style="Z-INDEX: 105; LEFT: 40px; POSITION: absolute; TOP: 24px" runat="server"
                Width
="248px" Font-Size="X-Small">股票输入(代码、拼音缩写、全词)</asp:label>
            
<Ajax:AjaxLookup id="axLookup1" style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 296px"
                Width
="128px" Runat="server" ItemStyleSpacing="10px" ItemStylePadding="1px 0px 1px 0px" ItemStyleBorderBottom="0px"
                FontWeight
="bold" FontSize="12px" BackgroundColor="#EEE" DivBorder="2px solid red" DivPadding="2px"
                DivFont
="Arial" HighlightColor="#C30" CallBackFunction="axlDemo.GetSearchItems" Visible="False" />
            
<asp:label id="Label2" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 264px" runat="server"
                Width
="96px" Visible="False">Control #2</asp:label>
            
<Ajax:AjaxLookup id="axLookup2" style="Z-INDEX: 104; LEFT: 264px; POSITION: absolute; TOP: 296px"
                Width
="128px" CallBackFunction="axlDemo.GetSearchItems" HighlightColor="gray" DivFont="Arial"
                DivPadding
="2px" DivBorder="4px solid blue" BackgroundColor="#EEE" FontSize="12px" FontWeight="bold"
                DivWidth
="200px" ItemStyleBorderBottom="0px" ItemStylePadding="1px 0px 1px 0px" ItemStyleSpacing="10px"
                Runat
="Server" Visible="False" />
        
</form>
    
</body>
</HTML>


附带示例:查询股票代码(分别可使用股票代码、名字拼音缩写名字来获取自动建议)
https://files.cnblogs.com/heekui/AjaxNET_Lookup_Control_2.rar
posted on 2006-08-14 00:10  网际浪人  阅读(1101)  评论(4编辑  收藏  举报