2013-01-05 23 views
0

我有一個帶有ItemTemplate,EditTemplate和InsertTemplate的ListView。 Edit和InsertTemplate中有幾個文本框。我需要計算Javascript中的一些值,但我不知道如何在我的JS函數中獲取ClientID或將它作爲參數傳遞。如何使用ListView和InsertTamplate或EditTemplate在JavaScript函數中獲取TextBox ClientID

JavaScript函數:

<script type="text/javascript"> 
    function calculate() { 
     var length = document.getElementById('???').value; 
     var quantity = document.getElementById('???').value; 
     var fullLength = document.getElementById('???'); 
     fullLength.value = length*quantity; 
    } 
</script> 

ASP.NET一塊我的ListView:

<InsertItemTemplate> 
    <asp:TextBox ID="Weight" runat="server" Text='<%#Bind("Weight") %>' /> 
    <asp:TextBox ID="Quantity" runat="server" Text='<%#Bind("Quantity") %>' /> 
    <asp:TextBox ID="FullLength" runat="server" Text='<%#Bind("FullLength") %>' /> 
    <asp:Button runat="server" ID="Insert" Text="AddNewEntry" CommandName="Insert" OnClientClick="calculate()" /> 
</InsertItemTemplate> 

是前人的精力,而不是什麼 '???'? 或者,我可以通過OnClientClick =「calculate(???,???,???)」中的參數以某種方式傳遞它?

由於提前,JiKra

回答

0

現在asp.net支持的ClientIDMode「可預見的」,你可以通過添加的OnClientClick處理器在EditItemTemplate中的更新按鈕做到這一點。

  • 將ListView的ClientIDMode設置爲可預測的。
  • 設置ListView的ClientIDRowSuffix到該行的主鍵(例如,產品ID爲羅斯文的Products表)
  • 將產品ID傳遞到的OnClientClick處理
  • 使用的document.getElementById獲取字段的當前值。

例如,假設您將ListView1綁定到Northwind的Products表。產品的主鍵是ProductID。爲ListView的聲明語法是:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="ProductID" 
     DataSourceID="SqlDataSource1" ClientIDMode="Predictable" 
     ClientIDRowSuffix="ProductID"> 

EditItemTemplate模板的更新按鈕的聲明語法是:

<asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
         Text="Update" OnClientClick=<%# string.Format("ShowProductName('{0}');", Eval("ProductID")) %> /> 

而且OnClientClick這個處理JavaScript是:

<script type="text/javascript"> 
    function ShowProductName(prodId) { 
     var prodNameTextBox = document.getElementById('ListView1_ProductNameTextBox_' + prodId); 

     alert('product name is ' + prodNameTextBox.value); 

     return false; 
    } 
</script> 

注意:假設產品名稱存儲在名爲ProductNameTextBox的文本框中。將其更改爲使文本框的名稱與所需字段相匹配。可預測模式將導致沿着ListView1_YOURFIELDNAMEHERE_PRODUCTIDHERE行的ID。

有關詳細信息,請參閱http://msdn.microsoft.com/en-us/library/dd410598(v=vs.100).aspx

+0

我不知道,如何發出的C#代碼JavaScript函數。您能提供一段代碼或鏈接到相關文章嗎? – JiKra

+0

添加了代碼片段和指向msdn的鏈接 –

相關問題