0

我一直在努力让所有这些组件正常工作,但是当我访问 js 时,它在 Student 页面上找不到 TextBox 控件。

MasterPage ScriptManager:

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

母版页 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 is Null。我尝试通过转到 ViewSource 来添加所有级别的占位符以找出如何找到该框并为其提供学生 ID 的值,但无济于事。事实上,它看起来是这样的:

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

有什么建议吗??

4

1 回答 1

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>

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

于 2013-02-07T22:59:58.587 回答