在.net中创建ajax程序出现'Sys' is undefined的解决方法

1. 在创建项目时,选择创建"ASP.NET AJAX-Enabled Web Application";

2. 在程序中的ajax控件中要用到的WebService, WebService的开头必须是这样

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    //[ToolboxItem(false)]

    [System.Web.Script.Services.ScriptService]

 

全部代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAXEnabledWebApplication1._Default" %>



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



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:ScriptManager ID="ScriptManager1" runat="server" />

    <div>

        <table>

            <tr>

                <td>

                </td>

                <td>

                </td>

                <td style="width: 637px">

                    <asp:Button ID="Button1" runat="server" Text="Button" />

                    

                </td>

            </tr>

            <tr>

                <td>

                </td>

                <td>

                </td>

                <td style="width: 637px">

                    <asp:TextBox ID="txtState" runat="server" autocomplete="off"></asp:TextBox>

                    

                    <ajaxToolkit:AutoCompleteExtender

                        runat="server" 

                        ID="autoComplete1" 

                        TargetControlID="txtState"

                        ServicePath="TestWS.asmx" 

                        ServiceMethod="GetCompletionList"

                        MinimumPrefixLength="2" 

                        CompletionInterval="1000"

                        EnableCaching="true"

                        CompletionSetCount="12" />

                    

                    <script type="text/javascript">

                        // Work around browser behavior of "auto-submitting" simple forms

                        var frm = document.getElementById("form1");

                        if (frm) {

                            frm.onsubmit = function() { return false; };

                        }

                    </script>

                    <%-- Prevent enter in textbox from causing the collapsible panel from operating --%>

                    <input type="submit" style="display:none;" />

                </td>

            </tr>

            <tr>

                <td>

                </td>

                <td>

                </td>

                <td style="width: 637px">

                </td>

            </tr>

        </table>

    

    </div>

    </form>

</body>

</html>
 
WebService代码:
using System;

using System.Data;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.ComponentModel;

using System.Collections.Generic;



namespace AJAXEnabledWebApplication1

{

    /// <summary>

    /// TestWS 的摘要说明

    /// </summary>

    [WebService(Namespace = "http://tempuri.org/")]

    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

    //[ToolboxItem(false)]

    [System.Web.Script.Services.ScriptService]



    public class TestWS : System.Web.Services.WebService

    {



        [WebMethod]

        public string HelloWorld()

        {

            return "Hello World";

        }



        [WebMethod]

        public string[] GetState(string prefixText, int count)

        {

            ArrayList al = new ArrayList();

            al.Add("ABCDEFG0");

            al.Add("ABCDEFG1");

            al.Add("ABCDEFG2");

            al.Add("ABCDEFG3");

            al.Add("ABCDEFG4");

            al.Add("ABCDEFG5");

            al.Add("ABCDEFG6");

            al.Add("ABCDEFG7");

            al.Add("ABCDEFG8");



            return (string[])al.ToArray(typeof(string));

        }



        [WebMethod]

        public string[] GetCompletionList(string prefixText, int count)

        {

            if (count == 0)

            {

                count = 10;

            }



            if (prefixText.Equals("xyz"))

            {

                return new string[0];

            }



            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(97, 122);



                items.Add(prefixText + c1 + c2 + c3);

            }



            return items.ToArray();

        }

    }

}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值