本文介绍了如何在客户端计算RadGrid单元格值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有处理编辑模式的telerik RadGrid。每个单元格都包含NumericTextBox。是否可以根据同一行中的其他单元格(在客户端)计算一个单元格。
例如,如果我有一行包含价格和项目,我希望在每次更改时计算总价格但在客户端,而不是去服务器端。 RadGrid有可能吗?

I have telerik RadGrid which is in edit mode. Each cell contains NumericTextBox. Is it possible to calculate one cell based on other cells in the same row (on client side). For example if I have a row which contains cells like price and item I want on every change to calculate total price but on client side, without going to server side. Is this possible with RadGrid?

推荐答案

感谢您的所有答案,但我在。我只是粘贴解决方案,以防有人遇到同样的问题。

Thanks for all your answers but I found the solution here at telerik forum. I'll just paste the solution here in case that somebody get stuck on the same issue.

ASPX:

<Columns> 
    <rad:GridTemplateColumn UniqueName="Price" HeaderText="Price">
    <EditItemTemplate> 
        <radI:RadNumericTextBox ID="txtPrice" runat="server">  
        </radI:RadNumericTextBox> 
    </EditItemTemplate> 
    </rad:GridTemplateColumn> 
    <rad:GridTemplateColumn UniqueName="Quantity" HeaderText=" Number of Items">  
    <EditItemTemplate> 
        <radI:RadNumericTextBox ID="txtQuantity" runat="server">  
        </radI:RadNumericTextBox> 
    </EditItemTemplate> 
    </rad:GridTemplateColumn> 
    <rad:GridTemplateColumn UniqueName="TotalAmount" HeaderText="Total">
    <EditItemTemplate> 
        <radI:RadNumericTextBox ID="txtTotalAmount" runat="server">  
        </radI:RadNumericTextBox> 
    </EditItemTemplate> 
    </rad:GridTemplateColumn> 
</Columns>

C#

  protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  

    if (e.Item is GridDataItem && e.Item.IsInEditMode)  
    {  
        GridDataItem item = (GridDataItem)e.Item;  
        RadNumericTextBox txtPrice= item.FindControl("txtPrice") as RadNumericTextBox;       // Get the textbox for column Price   
        RadNumericTextBox txtQuantity= item.FindControl("txtQuantity") as RadNumericTextBox;    // Get the textbox for column Quantity     
        RadNumericTextBox txtTotalAmount= item.FindControl("txtTotalAmount") as RadNumericTextBox; // Get the textbox for column "TotalAmount", if it is template as shown in aspx    

        txtPrice.Attributes.Add("onFocusout", "return calculate('" + txtPrice.ClientID + "','" + txtQuantity.ClientID + "','" + txtTotalAmount.ClientID + "')");  
        txtQuantity.Attributes.Add("onFocusout", "return calculate('" + txtPrice.ClientID + "','" + txtQuantity.ClientID + "','" + txtTotalAmount.ClientID + "')");  
        txtTotalAmount.Attributes.Add("onfocus", "return calculate('" + txtPrice.ClientID + "','" + txtQuantity.ClientID + "','" + txtTotalAmount.ClientID + "')");  
    }  
} 

JavaScript:

JavaScript:

<script type="text/javascript">  
function calculate(price, quantity, totalAmount)   
{  
    var text1 = $find(price); //I used Asp.net Ajax find method
    var text2 = $find(quantity);  
    var text3 = $find(totalAmount);  
    var total = text1.GetValue() * text2.GetValue();  
    text3.SetValue(total);  
}  
</script>

这篇关于如何在客户端计算RadGrid单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:29