使用服务和母版页自动完成

时间:2013-02-06 23:59:01

标签: asp.net ajax autocomplete

我一直在努力让所有这些组件正常工作,但是当我到达js时,它无法在学生页面上找到TextBox控件。

MasterPage ScriptManager:

<body>
<form id="MainForm" runat="server">
    <asp:ScriptManager ID="ScriptManagerMain" runat="server">
        <Services>
            <asp:ServiceReference Path="~/AutoComplete.asmx" />
        </Services>
    </asp:ScriptManager>

Masterpage ContentPlaceHolder:

     <asp:ContentPlaceHolder ID="ContentPlaceHolderStudent" runat="server">
        </asp:ContentPlaceHolder>

学生(儿童)页面:

<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolderStudent" runat="server" >
<asp:UpdatePanel ID="StudentSearchPanel" runat="server" ViewStateMode="Inherit">
    <ContentTemplate>
        Search by student ID:
        <br />
            <asp:TextBox ID="StudentIDSearchTextBox" runat="server" OnTextChanged="StudentIDSearchTextBox_TextChanged" Wrap="False" MaxLength="6" ClientIDMode="Static"  ViewStateMode="Inherit"></asp:TextBox>                 
        <br />
                    <asp:Label ID="StudentIDEntryError" runat="server" Visible="false" Font-Bold="True" ForeColor="Red" Text="Please enter a 6-digit student ID number."></asp:Label>
        <br />                        
        Or, begin typing student last name:
        <br />
        <asp:TextBox ID="StudentNameSearchTextBox" runat="server" 
            CssClass="StudentNameSearch"
            ViewStateMode="Inherit"></asp:TextBox>

学生页面上的自动完成扩展程序:

        <ajaxToolkit:AutoCompleteExtender ID="StudentNameSearchTextBox_AutoCompleteExtender" runat="server"
            TargetControlID="StudentNameSearchTextBox" 
            ServicePath="~/Scripts/AutoComplete.asmx"
            ServiceMethod="GetStudents"
            OnClientItemSelected="onStudentSelected" 
            MinimumPrefixLength="2" 
            CompletionSetCount="30" 
            UseContextKey="True" 
            CompletionListCssClass="autocomplete_completionListElement" 
            CompletionListHighlightedItemCssClass="autocomplete_highlightedListItem" 
            CompletionListItemCssClass="autocomplete_listItem" >
        </ajaxToolkit:AutoCompleteExtender>

AutoComplete.asmx正在运行,因此我不会添加该代码。当我开始输入学生姓氏时,我会得到一个json响应,如下所示(为了保护学生数据而改变)。我从FireBug中获取了这个:

{"d":["{\"First\":\"Lisaaa, Abby E.\",\"Second\":\"610999\"}","{\"First\":\"Lisbbb, Bobby P.\",\"Second\":\"151999\"}","{\"First\":\"Lisccc, Cathy M.\",\"Second\":\"739999\"}","{\"First\":\"Lisddd, David L.\",\"Second\":\"496999\"}","{\"First\":\"Liseee, Emily C.\",\"Second\":\"701999\"}","{\"First\":\"Lisfff, Frank G.\",\"Second\":\"623999\"}"]}

问题,当我到GetStudentJson.js中的OnClientItemSelected =“onStudentSelected”时:

function onStudentSelected(sender, e) {
    var selectedStudent = eval("(" + e._value + ")");
    var StudentIdTextbox = document.getElementById('<%=ContentPlaceHolderStudent_StudentIDSearchTextBox.ClientID %>');

    StudentIdTextbox.innerText = selectedStudent;
    __doPostBack(StudentIdTextbox, "");
}

我收到错误StudentIdTextbox为Null。我已经尝试通过转到ViewSource来添加所有级别的占位符,以找出如何找到该框并为其提供学生ID的值,但无济于事。事实上,这就是它的样子:

<input name="ctl00$ContentPlaceHolderStudent$StudentIDSearchTextBox" type="text" maxlength="6" id="StudentIDSearchTextBox" />

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我终于发现问题在于页面生命周期。我没有在主页面中引用脚本,而是将脚本本身移动到子页面。现在,'FindControl'正在寻找实际存在的控件。

<script type="text/javascript">
    function onStudentSelected(sender, e) {
        var selectedStudent = eval("(" + e._value + ")");
        var txtId = document.getElementById('<%= Page.Master.FindControl("ContentPlaceHolderStudent").FindControl("StudentSearchPanel").FindControl("StudentIDSearchTextBox").ClientID %>');
        txtId.value = selectedStudent;
    }
</script>

我只是想过早地找到控制权。