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

本文介绍了一款基于Ajax.Net的dll和WebUi的TextBox实现的AJAX文本框控件——AjaxLookup,该控件支持自动建议功能。通过简单的后台设置和前台属性配置,即可在网页上实现自动补全搜索功能,适用于多种应用场景。
AjaxLookup

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

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

后台:
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Text.RegularExpressions;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  Ajax;
None.gif
using  ThirdParty.Net;
None.gif
None.gif
namespace  axLookupDemo
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for _Default.
ExpandedSubBlockEnd.gif    
/// </summary>

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

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

InBlock.gif
InBlock.gif        [AjaxMethod()]
InBlock.gif        
public ArrayList GetSearchItemsCN(string query)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            query 
=  System.Web.HttpUtility.UrlDecode(query);
InBlock.gif            
InBlock.gif            
//测试如果是数字,按数字匹配
InBlock.gif
            
InBlock.gif            
if ( IsValidInt( query ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ArrayList items 
= GetRecordsCNByDM();
InBlock.gif                ArrayList matchItems 
= new ArrayList();
InBlock.gif
InBlock.gif                
if ( query.Length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                
InBlock.gif                    
foreach (string[] item in items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
if (item[0].StartsWith(query))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            matchItems.Add(item);
ExpandedSubBlockEnd.gif                        }

InBlock.gif
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
return matchItems;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else //测试非数字按拼音缩写或汉字查找
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
if ( IsValidEnString( query) ) //拼音缩写
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    ArrayList items 
= GetRecordsCNByPY();
InBlock.gif                    ArrayList matchItems 
= new ArrayList();
InBlock.gif
InBlock.gif                    
if ( query.Length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                
InBlock.gif                        
foreach (string[] item in items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (item[0].ToUpper().StartsWith(query.ToUpper()))
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
string[] itemOut = new string[2];
InBlock.gif                                itemOut[
0= item[1];
InBlock.gif                                itemOut[
1= item[2];
InBlock.gif
InBlock.gif                                matchItems.Add(itemOut);
ExpandedSubBlockEnd.gif                            }

InBlock.gif
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
return matchItems;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else //全词查找
ExpandedSubBlockStart.gifContractedSubBlock.gif
                dot.gif{
InBlock.gif                    ArrayList items 
= GetRecordsCNByDM();
InBlock.gif                    ArrayList matchItems 
= new ArrayList();
InBlock.gif
InBlock.gif                    
if ( query.Length > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                
InBlock.gif                        
foreach (string[] item in items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
if (item[1].StartsWith(query))
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                matchItems.Add(item);
ExpandedSubBlockEnd.gif                            }

InBlock.gif
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
return matchItems;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private ArrayList GetRecordsCNByDM()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList items 
= new ArrayList();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[,] item = new string[,] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"600616""G食品"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"600123""G瓜瓜"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"600601""G西西"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"600602""G西瓜"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"600102""G葡萄"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"600103""G枣子"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"600104""G哈密瓜"},
ExpandedSubBlockEnd.gif            }
;
InBlock.gif
InBlock.gif            
for ( int i = 0; i < item.GetLength(0); i ++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string[] itemLine = new string[item.GetLength(1)];
InBlock.gif                
InBlock.gif                
for ( int j = 0; j < itemLine.Length; j ++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    itemLine[j] 
= item[i,j].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                items.Add( itemLine );
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return items;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private ArrayList GetRecordsCNByPY()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ArrayList items 
= new ArrayList();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[,] item = new string[,] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"GSP""600616""G食品"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"GGG""600123""G瓜瓜"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"GXX""600601""G西西"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"GXG""600602""G西瓜"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"GPT""600102""G葡萄"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"GZZ""600103""G枣子"},
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                
dot.gif{"GHM""600104""G哈密瓜"},
ExpandedSubBlockEnd.gif            }
;
InBlock.gif
InBlock.gif            
for ( int i = 0; i < item.GetLength(0); i ++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string[] itemLine = new string[item.GetLength(1)];
InBlock.gif                
InBlock.gif                
for ( int j = 0; j < itemLine.Length; j ++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    itemLine[j] 
= item[i,j].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                items.Add( itemLine );
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return items;
ExpandedSubBlockEnd.gif        }

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

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

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

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


附带示例:查询股票代码(分别可使用股票代码、名字拼音缩写名字来获取自动建议)
http://www.cnblogs.com/Files/heekui/AjaxNET_Lookup_Control_2.rar

转载于:https://www.cnblogs.com/heekui/archive/2006/08/14/475933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值