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

来源:互联网 发布:c语言生成0~9随机数 编辑:程序博客网 时间:2024/04/29 12:58

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();        }    }}

 

 

 

 

 

原创粉丝点击