使用$get ASP.NET时出现错误

Getting Error vhen using $get ASP.NET

本文关键字:错误 NET get ASP 使用      更新时间:2023-09-26

在我的主页中,我插入了以下javascript

<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link rel="Stylesheet" type="text/css" href="css/MainStyle.css" />
<script type = "text/javascript">
function ClientItemSelected(sender, e) {
    $get("<%=hfCustomerId.ClientID %>").value = e.get_value();
}

在我的内容页中我有一个AutocompleteExtender这个这是代码

<asp:TextBox ID="txtCustomer" runat="server">
      </asp:TextBox>
      <ajx:AutoCompleteExtender ServiceMethod="SearchCustomers" MinimumPrefixLength="2"
          CompletionInterval="100" EnableCaching="false" CompletionSetCount="10" TargetControlID="txtCustomer"
          ID="AutoCompleteExtender1" runat="server" FirstRowSelected="false" OnClientItemSelected="ClientItemSelected">
      </ajx:AutoCompleteExtender>
      <asp:HiddenField ID="hfCustomerId" runat="server" />
       <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
          onclick="btnSubmit_Click"  />

但是当我编译时,我得到这个错误

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. 
Please review the following specific error details and modify your source code
appropriately. 
Compiler Error Message: CS0103: The name 'hfCustomerId' does not exist in the current context
Source Error:
Line 11:     <script type = "text/javascript">
Line 12:     function ClientItemSelected(sender, e) {
Line 13:         $get("<%=hfCustomerId.ClientID %>").value = e.get_value();
Line 14:         
Line 15:     }

有什么我错过了…?

hfCustomerId在aspx页面中,而不是在粘贴页面中,所以你不能直接引用它。

这里有一个指南:如何:参考ASP。. NET母版页内容

在实际控件(id为hfCustomerId的隐藏字段)存在的内容页上用javascript声明一个var,如:

 var ddlFldDataID = $("<%= ddlFldData.ClientID %>");

使用此变量代替母版页中的直接id,如

function ClientItemSelected(sender, e) {
    $('#' + ddlFldDataID.selector).val() = e.get_value();
}