How to calculate RadGrid cell value on Client side?

和自甴很熟 提交于 2019-12-04 19:41:14

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:

<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>

It is possible provided you got the data through client side binding as well, most likely through AJAX. If so, you should be able to get all values from the datasource property of the grid. If you bind the data server side it gets harder because currently the grid does not build the client side datasource in this case.

Check out the demo on the Telerik site from RadGrid -> Application scenarios section which uses numeric textboxes and illustrates what you are looking for, dude.

Dick

Because each item is in a table row you could use javascript to find the parent item and then loop through all the inputs to find the ones you are looking for? If you are going to use jQuery this should be possible and if you wanted to distinguish the textboxes easily you could apply a CssClass to each TextBox?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!