效果图片:(因为用QQ截图效果就没有了 没有上原版的)
WebService.asmx代码
WebService.asmx代码
using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//这使用这个Web签名必须使用System.Web.Script.Services.ScriptService
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
//if (count == 0)
//{
// count == 10;
//}
Random random = new Random();
List<string> items = new List<string>(count);
for (int i = 0; i < count; i++)
{
char c1 = (char)random.Next(65,90);
char c2 = (char)random.Next(97,122);
char c3 = (char)random.Next(48,57);
char c4 = (char)random.Next(33,43);
items.Add(prefixText + c1 + c2 + c3 + c4);
}
return items.ToArray();
}
}
using System.Web;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//这使用这个Web签名必须使用System.Web.Script.Services.ScriptService
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
//if (count == 0)
//{
// count == 10;
//}
Random random = new Random();
List<string> items = new List<string>(count);
for (int i = 0; i < count; i++)
{
char c1 = (char)random.Next(65,90);
char c2 = (char)random.Next(97,122);
char c3 = (char)random.Next(48,57);
char c4 = (char)random.Next(33,43);
items.Add(prefixText + c1 + c2 + c3 + c4);
}
return items.ToArray();
}
}
显示页面Htnl代码(注意这里的JavaScript脚本--回车触发指定按钮事件出发):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AJAX控件之AutoComplete</title>
<script type="text/javascript" language="javascript">
function SubmitKeyClick(button)
{
if (event.keyCode == 13)
{
event.keyCode=9;
event.returnValue = false;
document.all[button].click();
}
}
// -->
</script>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<br />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="MyAuto" runat="server" Width="240px"></asp:TextBox>
<asp:Button ID="Google"
runat="server" OnClick="Button1_Click" Text="Google搜索" Width="96px" />
<cc1:AutoCompleteExtender ID="ace" runat="server" CompletionInterval="100" MinimumPrefixLength="1" ServiceMethod="GetCompletionList" ServicePath="WebService.asmx" TargetControlID="MyAuto">
</cc1:AutoCompleteExtender>
<br />
<asp:Label ID="Label1" runat="server" Text="" Width="256px"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<br />
</div>
</form>
</body>
</html>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AJAX控件之AutoComplete</title>
<script type="text/javascript" language="javascript">
function SubmitKeyClick(button)
{
if (event.keyCode == 13)
{
event.keyCode=9;
event.returnValue = false;
document.all[button].click();
}
}
// -->
</script>
</head>
<body style="text-align: center">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<br />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="MyAuto" runat="server" Width="240px"></asp:TextBox>
<asp:Button ID="Google"
runat="server" OnClick="Button1_Click" Text="Google搜索" Width="96px" />
<cc1:AutoCompleteExtender ID="ace" runat="server" CompletionInterval="100" MinimumPrefixLength="1" ServiceMethod="GetCompletionList" ServicePath="WebService.asmx" TargetControlID="MyAuto">
</cc1:AutoCompleteExtender>
<br />
<asp:Label ID="Label1" runat="server" Text="" Width="256px"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<br />
</div>
</form>
</body>
</html>
显示页面的CS代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyAuto.Attributes.Add("onkeydown", "SubmitKeyClick('Google');");
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text ="你的信息为: <font color='red' size='3'>"+ MyAuto.Text+"</font>";
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyAuto.Attributes.Add("onkeydown", "SubmitKeyClick('Google');");
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text ="你的信息为: <font color='red' size='3'>"+ MyAuto.Text+"</font>";
}
}