0

我需要在我的网站上使用 JavaScript。当我创建新网页时,它可以与 JavaScript 一起正常工作。当我创建一个新网页时,它是从母版页派生的子页。此页面不支持我的 JavaScript。我将此代码用于多个单词的自动完成属性。

我的代码在这里:

标题中内容占位符中的 JavaScript 代码

<%@ Page Language="C#" MasterPageFile="~/Master_Front.master" AutoEventWireup="true"
    CodeFile="Mailbox.aspx.cs" Inherits="Mailbox" Title="Mail System" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">

    <link href="Style/ui-lightness/jquery-ui-1.8.21.custom.css"rel="stylesheet" type="text/css" />
    <script src="script/jquery.min.js" type="text/javascript"></script>
    <script src="script/jquery-ui.min.js" type="text/javascript"></script> 
        <script type="text/javascript">
        $(document).ready(function() {
            SearchText();
        });
        function SearchText() {
            $("#txtto").autocomplete({
                source: function(request, response) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        url: "Mailbox.aspx/GetAutoCompleteData",
                        data: "{'username':'" + extractLast(request.term) + "'}",
                        dataType: "json",
                        success: function(data) {
                            response(data.d);
                        },
                        error: function(result) {
                            alert("Error");
                        }
                    });
                },
                focus: function() {
                    // prevent value inserted on focus
                    return false;
                },
                select: function(event, ui) {
                    var terms = split(this.value);
                    // remove the current input
                    terms.pop();
                    // add the selected item
                    terms.push(ui.item.value);
                    // add placeholder to get the comma-and-space at the end
                    terms.push("");
                    this.value = terms.join(", ");
                    return false;
                }
            });
            $("#txtto").bind("keydown", function(event) {
                if (event.keyCode === $.ui.keyCode.TAB &&
                        $(this).data("autocomplete").menu.active) {
                    event.preventDefault();
                }
            })
            function split(val) {
                return val.split(/,\s*/);
            }
            function extractLast(term) {
                return split(term).pop();
            }
        }
    </script>
</asp:Content>

C#代码:

[WebMethod]
public static List<string> GetAutoCompleteData(string user_name)
{
    List<string> result = new List<string>();
    SqlDataReader dr=General.ReturnDR("select DISTINCT mailid from UserDetails where mailid LIKE '%"+user_name+"%'");
    while (dr.Read())
    {
        result.Add(dr["mailid"].ToString());
    }
    return result;
}
4

1 回答 1

1

您可以将所有脚本放在document.ready中,以便在脚本访问元素时准备好元素。

$(document).ready(function(){
    //put all your script of child page here.
});
于 2013-04-02T04:48:11.797 回答