{A}{S0}简介
这是非常简单的代码,这使得自动与数据库的完整组合。
这是非常有用。首先,你不必了解Ajax功能,只要下载AJAX控件工具包CodePlex上,并按照我,然后享受。此外,当有许多行,你可以在文本框中键入单词的一部分,那么它可以提供所有类似的话。背景:什么是Ajaxcontroltoolkit?
ASP.NET AJAX控件工具包是一个开源项目的Microsoft ASP.NET AJAX框架之上,并包含超过30个控制,使您可以轻松地创建丰富,交互式网页。如果你想了解它的更多信息,请访问。使用代码
它的第一步,你必须下载} {A3 AjaxControlToolkit。
你必须去和ajaxcontroltoolkit下载,然后复制ajaxcontroltoolkit并将其粘贴到bin文件夹,右键单击解决方案,选择添加引用,在Bin文件夹中浏览"选项卡,双击,双击ajaxcontroltoolkit,然后在生成菜单上的GT;单击重建。数据库新建查询CREATE TABLE [dbo].[tblCustomer](
[CompanyName] [nvarchar](500) NULL,
[ID] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]
insert into dbo.tblCustomer(CompanyName) values('calemard')
insert into dbo.tblCustomer(CompanyName) values('dantherm')
insert into dbo.tblCustomer(CompanyName) values('dango dienenthal')
insert into dbo.tblCustomer(CompanyName) values('daewoo')
insert into dbo.tblCustomer(CompanyName) values('daim engineering')
的Visual Studio 2008 - NET 3.5:创建网站和它命名为"自动完成",创建Web窗体,并将其命名它AutoComplete.aspx,在HTML视图,编写该代码
,但有一个C#和VB之间的差别不大。在这一节。这在底部的代码为C#编码。如果你是一个VB程序员,请修改页面标签2节一:正确的语言= VB二:正确的CodeFile ="; AutoComplete.aspx.vbquot";{C}对于样式表(CSS文件)创建样式表:解决方案GT;右键GT;添加新项GT; Web服务GT;产品名称:stylesheet.css中语言:Visual Basic或C#GT; stylesheet.css中(档案)GT CTRL A(全选)GT;删除本节(见下文)GT;选择此代码GT; CTRL C GT;stylesheet.css中(文件)GT; CTRL V(粘贴)
对于VB创建Web服务:解决方案GT;右键GT;添加新项GT; Web服务GT;产品名称:AutoComplete.asmx语言:Visual Basic中到GT的App_Code GT; AutoComplete.vb
对于C#Web服务解决方案:GT;右键GT,添加新的项目GT; Web服务GT;产品名称:AutoComplete.asmx语言:C#到GT的App_Code GT; AutoComplete.cs/*AutoComplete flyout */
.autocomplete_completionListElement
{
margin : 0px!important ;
background-color : inherit ;
color : windowtext ;
border : buttonshadow ;
border-width : 1px ;
border-style : solid ;
cursor : 'default' ;
overflow : auto ;
height : 200px ;
font-family : Tahoma ;
font-size : small ;
text-align : left ;
list-style-type : none ;
}
/* AutoComplete highlighted item */
.autocomplete_highlightedListItem
{
background-color : #ffff99 ;
color : black ;
padding : 1px ;
}
/* AutoComplete item */
.autocomplete_listItem
{
background-color : window ;
color : windowtext ;
padding : 1px ;
}
摘要// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
///<summary>
/// Summary description for AutoComplete
///</summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX,
// uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {
public AutoComplete () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
//ADO.Net
SqlConnection cn =new SqlConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
String strCn = "data source=.;Initial Catalog=MyDB;Integrated Security=True";
cn.ConnectionString = strCn;
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
//Compare String From Textbox(prefixText) AND String From
//Column in DataBase(CompanyName)
//If String from DataBase is equal to String from TextBox(prefixText)
//then add it to return ItemList
//-----I defined a parameter instead of passing value directly to
//prevent SQL injection--------//
cmd.CommandText = "select * from tblCustomer Where CompanyName like @myParameter";
cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");
try
{
cn.Open();
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
catch
{
}
finally
{
cn.Close();
}
dt = ds.Tables[0];
//Then return List of string(txtItems) as result
List<string> txtItems =new List<string>();
String dbValues;
foreach (DataRow row in dt.Rows)
{
//String From DataBase(dbValues)
dbValues = row["CompanyName"].ToString();
dbValues = dbValues.ToLower();
txtItems.Add(dbValues);
}
return txtItems.ToArray();
}
}
GetCompletionList是一个函数,渔获量作为字符串参数,prefixText和计数为int。
当你键入一些字符,它们将被保存在prefixText和字符的数量计数保存。和结束时,函数返回的字符串列表(你的角色,它们是相似的)已获得如下:
我写了一些代码ADO.NET第prefixText,这是您键入的字符在文本框中。
此外,这些行已筛选,我定义了一个参数,而不是直接传递价值防止SQL注入。
我创建字符串数据类型的列表txtItems,我们可以节省的话,我们希望。然后,我在foreach循环中转换成tolower的, 我添加这些值,我的结果值(txtItems),我终于回到txtItems。尝试分步转到 andnbsp下载AjaxControlToolkit文件。复制的文件夹quot; AjaxControlToolkit.Dllquot;所有dependers,有18个对象,您的网站的Bin文件夹(C:\自动完成\ BIN)。右键单击解决方案,然后选择"刷新",然后点击右键再次单击"添加引用",然后单击"浏览"选项卡,双击Bin文件夹中,并双击上ajaxcontroltoolkit,生成菜单上的GT,单击重建。Createnbsp;数据库和表像上面,并添加一些常用词的行。创建Web窗体,并命名为:"AutoComplete.aspxquot;在HTML视图,写一些像上面的代码。 (这应该是完全一样,因为这部分是大小写敏感的,我的代码)。创建一个WebService:
解决方案GT;右键GT GT的新项目; Web服务GT名称:AutoComplete.asmx语言:C#或VB
到GT的App_Code GT; AutoComplete.cs对于一些动画效果,我添加了样式表,使用它用户友好。如果你是一个VB程序员,使用thenbsp; VB范例,否则,使用C#范例。运行程序,并在文本框中写一个字,包含2个或更多字符作为DA,你会看到一个列表的话,你的性格是相似的。反馈
感觉自由离开任何对本文的反馈,它是很高兴看到yournbsp;意见和表决有关此代码。如果您有任何问题,请不要犹豫,问我在这里。