1.Demo下載:
2.創建數據庫、表(我用的sqlserver2008數據庫)

1 CREATE TABLE Ceshi 2 ( 3 id VARCHAR(50) PRIMARY KEY NOT NULL, 4 cname VARCHAR(30) 5 ) 6 GO 7 INSERT INTO Ceshi 8 SELECT NEWID(),'jack1' UNION 9 SELECT NEWID(),'jack2' UNION 10 SELECT NEWID(),'jack3' UNION 11 SELECT NEWID(),'jack4' UNION 12 SELECT NEWID(),'jack5' UNION 13 SELECT NEWID(),'peter1' UNION 14 SELECT NEWID(),'peter2' UNION 15 SELECT NEWID(),'peter3' UNION 16 SELECT NEWID(),'peter4' UNION 17 SELECT NEWID(),'peter5' UNION 18 SELECT NEWID(),'姜某某' UNION 19 SELECT NEWID(),'江某某' 20 go
3.創建自定義函數

1 create function [dbo].[f_GetPy](@str nvarchar(4000)) 2 returns nvarchar(4000) 3 as 4 begin 5 declare @strlen int,@re nvarchar(4000) 6 declare @t table(chr nchar(1) collate Chinese_PRC_CI_AS,letter nchar(1)) 7 insert into @t(chr,letter) 8 select '吖 ', 'A ' union all select '八 ', 'B ' union all 9 select '嚓 ', 'C ' union all select '咑 ', 'D ' union all 10 select '妸 ', 'E ' union all select '發 ', 'F ' union all 11 select '旮 ', 'G ' union all select '鉿 ', 'H ' union all 12 select '丌 ', 'J ' union all select '咔 ', 'K ' union all 13 select '垃 ', 'L ' union all select '嘸 ', 'M ' union all 14 select '拏 ', 'N ' union all select '噢 ', 'O ' union all 15 select '妑 ', 'P ' union all select '七 ', 'Q ' union all 16 select '呥 ', 'R ' union all select '仨 ', 'S ' union all 17 select '他 ', 'T ' union all select '屲 ', 'W ' union all 18 select '夕 ', 'X ' union all select '丫 ', 'Y ' union all 19 select '帀 ', 'Z ' 20 select @strlen=len(@str),@re= ' ' 21 while @strlen> 0 22 begin 23 select top 1 @re=letter+@re,@strlen=@strlen-1 24 from @t a where chr <=substring(@str,@strlen,1) 25 order by chr desc 26 if @@rowcount=0 27 select @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1 28 end 29 return(@re) 30 end 31 GO
4.asp.net前台頁面(需要添加2個引用:AjaxControlToolkit.dll,AutoCompleteExtra.dll)

1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextBoxAuto.aspx.cs" Inherits="WebApplication1.TextBoxAuto" %> 2 3 <%@ Register Assembly="AutoCompleteExtra" Namespace="AutoCompleteExtra" TagPrefix="cc1" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 5 <html xmlns="http://www.w3.org/1999/xhtml"> 6 <head runat="server"> 7 <title></title> 8 <style type="text/css"> 9 .searchTextBox 10 { 11 border: 1px solid #e1e1e1; 12 border-collapse: separate; 13 border-spacing: 0; 14 padding: 2px 2px 2px 2px; 15 white-space: nowrap; 16 margin-left: 2px; 17 height: 28px; 18 line-height: 28px; 19 margin-right: 5px; 20 font-family: 微軟雅黑,宋體; 21 font-size: 14px; 22 } 23 </style> 24 </head> 25 <body> 26 <form id="form1" runat="server"> 27 <asp:ScriptManager ID="ScriptManager1" runat="server"> 28 </asp:ScriptManager> 29 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 30 <ContentTemplate> 31 <div> 32 <div class="dd2"> 33 請輸入姓名: <asp:TextBox CssClass="searchTextBox" runat="server" ID="txtCompanyName" Style="width: 280px;"></asp:TextBox> 34 <cc1:AutoCompleteExtraExtender ID="AutoCompleteExtraExtender1" runat="server" ServiceMethod="GetCompanyNameList" 35 TargetControlID="txtCompanyName" AsyncPostback="false" UseContextKey="True" AutoPostback="false" 36 MinimumPrefixLength="1" CompletionInterval="10"> 37 </cc1:AutoCompleteExtraExtender> 38 </div> 39 </div> 40 </ContentTemplate> 41 </asp:UpdatePanel> 42 </form> 43 </body> 44 </html>
5.后台頁面

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Oceansoft.Net.Bll; 8 9 namespace WebApplication1 10 { 11 public partial class TextBoxAuto : System.Web.UI.Page 12 { 13 protected void Page_Load(object sender, EventArgs e) 14 { 15 16 } 17 18 [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()] 19 public static string[][] GetCompanyNameList(string prefixText, int count, string contextKey) 20 { 21 //獲取自動完成的選項數據 22 List<string[]> list = new List<string[]>(); 23 List<string> nameList = new List<string>(); 24 List<string> idList = new List<string>(); 25 CeshiManage ceshimanage = new CeshiManage(); 26 27 ceshimanage.GetTopUserName(count, prefixText.ToUpper(), out idList, out nameList); 28 for (int i = 0; i < nameList.Count; i++) 29 { 30 string[] Respuesta = new string[2]; 31 Respuesta[0] = nameList[i]; 32 Respuesta[1] = idList[i]; 33 list.Add(Respuesta); 34 } 35 return list.ToArray(); 36 } 37 } 38 }
6.后台頁面用到的方法(管理類)

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web.UI; 6 using Oceansoft.Net.Bll; 7 using SubSonic; 8 using System.Transactions; 9 10 11 using System.Data; 12 using Oceansoft.Net.Dal; 13 14 15 16 namespace Oceansoft.Net.Bll 17 { 18 /// <summary> 19 /// :ceshi 20 /// :jibp 21 /// :2014-2-27 15:52:15 22 ///</summary> 23 public class CeshiManage 24 { 25 26 private SqlQuery m_sqlquery = Oceansoft.Net.Dal.DB.Select().From(Ceshi.Schema); 27 28 /// <summary> 29 /// Ceshi查詢器 30 /// </summary> 31 public SqlQuery CeshiSelecter 32 { 33 get { return m_sqlquery; } 34 set { m_sqlquery = value; } 35 } 36 37 38 /// <summary> 39 /// 構造函數,設置查詢器 40 ///</summary> 41 public CeshiManage() 42 { 43 m_sqlquery = m_sqlquery.Where("id").IsNotEqualTo(""); 44 } 45 46 47 #region Ceshi管理 48 49 /// <summary> 50 /// 獲取ceshi列表 51 /// </summary> 52 /// <returns></returns> 53 public List<Ceshi> getCeshiList() 54 { 55 56 return CeshiSelecter.ExecuteTypedList<Ceshi>(); 57 } 58 59 60 /// <summary> 61 /// 獲取ceshi列表,同時分頁操作 62 /// </summary> 63 /// <returns></returns> 64 public List<Ceshi> getCeshiList(int currentPage, int pageSize, out int RecordCount) 65 { 66 RecordCount = m_sqlquery.GetRecordCount(); 67 return CeshiSelecter 68 .Paged(currentPage, pageSize) 69 .ExecuteTypedList<Ceshi>(); 70 } 71 72 73 74 75 76 /// <summary> 77 /// 新增 ceshi 78 /// </summary> 79 /// <param name="HandleEntity"></param> 80 /// <param name="sErr"></param> 81 /// <returns></returns> 82 public bool AddCeshi(Ceshi beAddMode, out string sErr) 83 { 84 85 sErr = ""; 86 bool bRet = true; 87 try 88 { 89 90 using (TransactionScope sc = new TransactionScope()) 91 { 92 //此處寫代碼 93 //流水編號的生成 94 //GenerateNo No = new GenerateNo(); 95 //No.TableName = "Ceshi"; //表名 96 //No.NoName = "XXX"; //流水號前字母 97 //No.ColName = "CC_Number"; //編號字段 98 //No.CreateTime = "CC_CreateTime"; //日期字段 99 //string BillNo = ""; 100 //Customer_Comp.CC_Number = No.AutoGenerateNo(); 101 beAddMode.IsNew = true; 102 beAddMode.Save(); 103 //LogHelper.WriteLog(logType.新增 , logModule.Deptrelation,"ceshi新增成功("+beAddMode.GetPrimaryKeyValue().ToString() 104 //+")!"); 105 //如果生成擴展類請使用add方法方法 106 sc.Complete(); 107 } 108 } 109 catch (Exception ex) 110 { 111 sErr = "ceshi新增不成功!"; 112 return false; 113 } 114 115 sErr = "ceshi新增成功!"; 116 return bRet; 117 118 119 } 120 121 122 123 /// <summary> 124 /// 修改 ceshi 125 /// </summary> 126 /// <param name="HandleEntity"></param> 127 /// <param name="sErr"></param> 128 /// <returns></returns> 129 public bool UpdataCeshi(Ceshi beUpdataMode, out string sErr) 130 { 131 132 sErr = ""; 133 bool bRet = true; 134 try 135 { 136 137 using (TransactionScope sc = new TransactionScope()) 138 { 139 140 //如果生成擴展類請使用Update()方法方法 141 beUpdataMode.IsNew = false; 142 beUpdataMode.Save(); 143 //LogHelper.WriteLog(logType.修改 , logModule.Deptrelation,"ceshi修改成功("+beUpdataMode.GetPrimaryKeyValue().ToString() 144 //+")!"); 145 146 sc.Complete(); 147 } 148 } 149 catch (Exception ex) 150 { 151 sErr = "ceshi修改不成功!"; 152 return false; 153 } 154 155 sErr = "ceshi修改成功!"; 156 return bRet; 157 158 } 159 160 161 162 163 /// <summary> 164 /// 刪除 ceshi 165 /// </summary> 166 /// <param name="HandleEntity"></param> 167 /// <param name="sErr"></param> 168 /// <returns></returns> 169 public bool DeleteCeshi(Ceshi beDeleteMode, out string sErr) 170 { 171 sErr = ""; 172 bool bRet = true; 173 try 174 { 175 176 using (TransactionScope sc = new TransactionScope()) 177 { 178 //如果生成擴展類請使用Delete()方法方法 179 Ceshi.Delete(beDeleteMode.GetPrimaryKeyValue()); 180 //LogHelper.WriteLog(logType.刪除 , logModule.Deptrelation,"ceshi刪除成功("+beDeleteMode.GetPrimaryKeyValue().ToString() 181 //+")!"); 182 sc.Complete(); 183 } 184 } 185 catch (Exception ex) 186 { 187 sErr = "ceshi刪除不成功!"; 188 return false; 189 } 190 191 sErr = "ceshi刪除成功!"; 192 return bRet; 193 194 } 195 196 197 /// <summary> 198 /// 刪除 ceshi 列表 199 /// </summary> 200 /// <param name="HandleEntity"></param> 201 /// <param name="sErr"></param> 202 /// <returns></returns> 203 public bool DeleteCeshiList(List<Ceshi> lstCeshi, out string sErr) 204 { 205 206 207 sErr = ""; 208 int ii = 0; 209 bool bRet = true; 210 try 211 { 212 213 using (TransactionScope sc = new TransactionScope()) 214 { 215 //如果生成擴展類請使用Delete()方法方法 216 foreach (Ceshi bedelmode in lstCeshi) 217 { 218 ii++; 219 Ceshi.Delete(bedelmode.GetPrimaryKeyValue()); 220 221 //LogHelper.WriteLog(logType.刪除 , logModule.Deptrelation,"ceshi刪除成功("+bedelmode.GetPrimaryKeyValue().ToString() 222 //+")!"); 223 } 224 sc.Complete(); 225 } 226 } 227 catch (Exception ex) 228 { 229 sErr = "ceshi刪除不成功!"; 230 return false; 231 } 232 233 sErr = "共" + ii.ToString() + "條單據刪除成功!"; 234 return bRet; 235 236 237 238 239 } 240 241 242 243 public void GetTopUserName(int topCount, string name, out List<string> listId, out List<string> listcname) 244 { 245 string sql = string.Format(@"Select id,cname from(Select ROW_NUMBER() over(order by cname)as ROWNUM," + 246 "id,cname FROM [dbo].[Ceshi] where cname like '%" + name + "%' or dbo.f_GetPy(cname) like '%" + name + "%') as ta where ta.ROWNUM <= " + topCount); 247 DataTable dt = new DataTable(); 248 QueryCommand qc = new InlineQuery().GetCommand(sql); 249 dt = DataService.GetDataSet(qc).Tables[0];//將查詢出來的數據集放到List中去(查詢數據的方法,有很多,這邊我用的是Subsonic類自帶的查詢方法) 250 listcname = new List<string>(); 251 listId = new List<string>(); 252 foreach (DataRow row in dt.Rows) 253 { 254 255 listId.Add(row[0].ToString()); 256 listcname.Add(row[1].ToString()); 257 258 } 259 260 } 261 262 263 #endregion 264 265 266 267 268 } 269 }
7.webconfig配置

1 <?xml version="1.0"?> 2 3 <!-- 4 有關如何配置 ASP.NET 應用程序的詳細信息,請訪問 5 http://go.microsoft.com/fwlink/?LinkId=169433 6 --> 7 8 <configuration> 9 <configSections> 10 <section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" requirePermission="false"/> 11 </configSections> 12 <connectionStrings> 13 <add name="DemoTo" connectionString="Data Source=172.17.118.197;Initial Catalog=DemoTo;User Id=sa;Password=password01!;" providerName="System.Data.SqlClient"/> 14 </connectionStrings> 15 <SubSonicService defaultProvider="DemoTo"> 16 <providers> 17 18 <add name="DemoTo" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="DemoTo" generatedNamespace="Oceansoft.Net" maxPoolSize="2000"/> 19 20 </providers> 21 </SubSonicService> 22 23 <system.web> 24 <compilation debug="true" targetFramework="4.0" /> 25 26 <authentication mode="Forms"> 27 <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> 28 </authentication> 29 30 <membership> 31 <providers> 32 <clear/> 33 <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" 34 enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" 35 maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" 36 applicationName="/" /> 37 </providers> 38 </membership> 39 40 <profile> 41 <providers> 42 <clear/> 43 <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> 44 </providers> 45 </profile> 46 47 <roleManager enabled="false"> 48 <providers> 49 <clear/> 50 <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> 51 <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> 52 </providers> 53 </roleManager> 54 55 </system.web> 56 57 <system.webServer> 58 <modules runAllManagedModulesForAllRequests="true"/> 59 </system.webServer> 60 </configuration>