0

嗨,在我的母版页中,我插入了以下 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:     }

有什么我想念的..?

4

2 回答 2

0

在实际控制(ID为hfCustomerId的隐藏字段)存在的内容页面上用javascript声明一个var,如下所示:

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

使用此变量而不是母版页中的直接 ID,例如

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

请注意,您必须引用 jquery。

于 2013-05-29T10:50:22.003 回答
0

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

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

于 2013-05-29T09:31:06.417 回答