websevice 服务前台和后台

<%@ Page Title="登录" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Login.aspx.cs" Inherits="PDMv01.Account.Login" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>


<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <h2>
        登录
    
        </h2>
    <p>
        请输入用户名和密码。
        <asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">注册</asp:HyperLink> 如果您没有帐户。账号只限于巨涛用户,用户名为您的中文名,邮箱为您巨涛的邮箱。</p>
    <table style="100%;">
        <tr>
            <td>
                &nbsp;</td>
            <td>
    <asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
        <LayoutTemplate>
            <span class="failureNotification">
                <asp:Literal ID="FailureText" runat="server"></asp:Literal>
            </span>
            <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
                 ValidationGroup="LoginUserValidationGroup"/>
            <div class="accountInfo">
                <fieldset class="login">
                    <legend>帐户信息</legend>
                    <p>
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">用户名:</asp:Label>
                        <asp:TextBox ID="UserName" runat="server" CssClass="textEntry" Width="200px"></asp:TextBox>
                        <ajaxToolkit:AutoCompleteExtender ID="ac1" runat="server" TargetControlID="UserName"
                         ServicePath="~/webservice.asmx" ServiceMethod="GetData" MinimumPrefixLength="1"
                           CompletionSetCount="100" ></ajaxToolkit:AutoCompleteExtender>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                             CssClass="failureNotification" ErrorMessage="必须填写“用户名”。" ToolTip="必须填写“用户名”。"
                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">密码:</asp:Label>
                        <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry"
                            TextMode="Password" Width="200px"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                             CssClass="failureNotification" ErrorMessage="必须填写“密码”。" ToolTip="必须填写“密码”。"
                             ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:CheckBox ID="RememberMe" runat="server"/>
                        <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">保持登录状态</asp:Label>
                    </p>
                      <p class="submitButton">
                    <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="登录" ValidationGroup="LoginUserValidationGroup"/>
                </p>
                </fieldset>
                <div>
                </div>
             
            </div>
        </LayoutTemplate>
    </asp:Login>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
              </td>
         
                           
            <td>
             <fieldset class="忘记密码?">
            <p>
                <asp:PasswordRecovery ID="PasswordRecovery1" runat="server" >
                </asp:PasswordRecovery>
             </p>
                 
             </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;</td>
            <td>
              
                </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    <br />
</asp:Content>

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;//关键引用
using System.Collections.Generic;//关键引用
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

namespace PDMv01
{
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
     [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        public WebService()
        {

            //如果使用设计的组件,请取消注释以下行
            //InitializeComponent();
        }

        private string[] autoCompleteWordList = null;
        private readonly string myConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString;


        [WebMethod]
        public string[] GetData(string prefixText, int count)
        {

            ///检测参数是否为空

            if (string.IsNullOrEmpty(prefixText) == true || count <= 0) return null;

            // 如果数组为空

            if (autoCompleteWordList == null)
            {

                //读取数据库的内容

                SqlConnection conn = new SqlConnection(myConnectionString);


                conn.Open();

                SqlDataAdapter da = new SqlDataAdapter("select UserName from [vw_aspnet_MembershipUsers] where UserName like'" + prefixText + "%' order by UserName", conn);

                DataSet ds = new DataSet();

                da.Fill(ds);

                //读取内容文件的数据到临时数组

                string[] temp = new string[ds.Tables[0].Rows.Count];

                int i = 0;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {

                    temp[i] = dr["UserName"].ToString();

                    i++;

                }

                Array.Sort(temp, new CaseInsensitiveComparer());

                //将临时数组的内容赋给返回数组

                autoCompleteWordList = temp;

                if (conn.State == ConnectionState.Open)

                    conn.Close();

            }

            //定位二叉树搜索的起点

            int index = Array.BinarySearch(autoCompleteWordList, prefixText, new CaseInsensitiveComparer());

            if (index < 0)
            { //修正起点

                index = ~index;

            }

            //搜索符合条件的数据

            int matchCount = 0;

            for (matchCount = 0; matchCount < count && matchCount + index < autoCompleteWordList.Length; matchCount++)
            { ///查看开头字符串相同的项

                if (autoCompleteWordList[index + matchCount].StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) == false)
                {

                    break;

                }

            }

            //处理搜索结果

            string[] matchResultList = new string[matchCount];

            if (matchCount > 0)
            { //复制搜索结果

                Array.Copy(autoCompleteWordList, index, matchResultList, 0, matchCount);

            }

            return matchResultList;


        }

    }
}

原文地址:https://www.cnblogs.com/wycoo/p/2325502.html