2010-05-17 67 views
1

我安裝了Asp.net Ajax工具包。在我的網站中,我使用該工具包中的NummericUpDown。 現在,我希望標籤在NummericUpDown文本框更改時進行更改。我嘗試使用JavaScript這一點,但我總是得到以下錯誤:Asp.net Ajax問題

'ASP.orders_aspx' does not contain a definition for 'changeAmount' and no extension method 'changeAmount' accepting a first argument of type 'ASP.orders_aspx' could be found (are you missing a using directive or an assembly reference?) 



這是我的代碼:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" 
    CodeFile="orders.aspx.cs" Inherits="orders" Title="the BookStore" %> 

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 

    <script type="text/javascript"> 

     function changeAmount() 
     { 
      var amount = document.getElementById("txtCount"); 
      var total = 10 * amount.value; 

      document.getElementById("lblPrice").value = total; 
     } 


    </script> 

</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
    <ajaxToolkit:ToolkitScriptManager runat="Server" EnablePartialRendering="true" ID="ScriptManager1" /> 

    <h1 id="H1" runat="server"> 
     Bestellen</h1> 

    <asp:Panel ID="pnlZoeken" runat="server" Visible="true"> 

     <asp:ObjectDataSource ID="objdsSelectedBooks" runat="server" OnSelecting="objdsSelectedBooks_Selecting" 
      TypeName="DAL.BooksDAL"></asp:ObjectDataSource> 
     <h3> 
      Overzicht van het gekozen boek</h3> 
     <asp:FormView ID="fvBestelBoek" runat="server" Width="650"> 
      <ItemTemplate> 
       <h3> 
        Aantal boeken bestellen</h3> 
       <table width="650"> 
        <tr class="txtBox"> 
         <td> 
          Boek 
         </td> 
         <td> 
          Prijs 
         </td> 
         <td> 
          Aantal 
         </td> 
         <td> 
          Korting 
         </td> 
         <td> 
          Totale Prijs 
         </td> 
        </tr> 
        <tr> 
         <td> 
          <%# Eval("TITLE") %> 
         </td> 
         <td> 
          <asp:Label ID="lblPrice" runat="server" Text='<%# Eval("PRICE") %>' /> 
         </td> 
         <td> 
          <asp:TextBox OnTextChanged="changeAmount()" ID="txtCount" runat="server"></asp:TextBox> 
          <ajaxToolkit:NumericUpDownExtender ID="NumericUpDownExtender1" runat="server" TargetControlID="txtCount" 
           Width="50" Minimum="1" ServiceDownMethod="" ServiceUpMethod="" /> 
         </td> 
         <td> 
          - 
         </td> 
         <td> 
          <asp:Label ID="lblAmount" runat="server" /> 
         </td> 
        </tr> 
       </table> 
      </ItemTemplate> 
     </asp:FormView> 

     <asp:Button ID="btnBestel" runat="server" CssClass="btn" Text="Bestel" OnClick="btnBestel_Click1" /> 
     <asp:Button ID="btnReChoose" runat="server" CssClass="btnDelete" Text="Kies een ander boek" 
      OnClick="btnRechoose_Click" /> 
    </asp:Panel> 
</asp:Content> 

有誰知道答案嗎?

非常感謝,文森特

回答

2

你試圖分配一個客戶端的方法到OnTextChanged事件,這是一個服務器端事件。

此外,您的JavaScript引用標籤和文本框的服務器ID。 WinForms引擎會根據嵌套控件的位置附加字符。使用<%=myControl.ClientID%>來解決這個問題。

您需要使用一些純JavaScript此:

window.onload=function(){ 
    //assign client method to textbox 
    document.getElementById('<%=txtCount.ClientID%>').onChange = function(){ 
     var amount = document.getElementById('<%=txtCount.ClientID%>'); 
     var total = 10 * amount.value; 

     document.getElementById('<%=lblPrice.ClientID%>').value = total; 

    } 
    } 

地方,你的頁面,而不是當前的Javascript和上擺脫OnTextChanged="changeAmount()"屬性。

+0

非常感謝這個答案;) – Vinzcent 2010-05-17 13:46:43

1

您爲TextBox設置了OnTextChanged。它是指服務器方法,即在PostBack上觸發。

如果要在頁面上使用方法處理背景中的Up和Down事件,則必須爲NumericUpDown標記設置 屬性ServiceUpPath和ServiceUpMethod。

如果要處理客戶端事件 - 設置自定義向上和向下按鈕並將屬性OnClientClick設置爲它們。

http://www.asp.net/AJAX/AjaxControlToolkit/Samples/NumericUpDown/NumericUpDown.aspx

+0

而且,謝謝:) – Vinzcent 2010-05-17 13:46:28