如何在子页面上使用document.ready函数

时间:2013-04-02 04:46:52

标签: c# javascript asp.net master-pages document-ready

我需要在我的网站上使用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;
}

1 个答案:

答案 0 :(得分:1)

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

$(document).ready(function(){
    //put all your script of child page here.
});