我正在使用RadGrid。
我按照这篇文章@StackOverflow调用了一个javascript方法对单元格进行了一些计算,并更新了同一行上的另一个单元格。
它调用Calculate方法,但单元格值第一次不可用。如果单元格再次获得焦点,则该值将显示在Calculate()中。所有行都是这种情况。
使用第二个onBlur事件上的计算值更新目标单元格,因为第一个onBlur未带来编辑的值。但是当焦点转移到任何其他单元格时,目标单元格会丢失它的更新值并返回到原始值。我没有在任何其他单元事件上调用任何其他javascript方法。
我的要求是计算例如,qtyCell和costCell和 在客户端更新amountCell。
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
RadTextBox txtQty = item.FindControl("qty") as RadTextBox;
RadTextBox txtAmount = item.FindControl("valued") as RadTextBox;
RadTextBox txtUnitCost = item.FindControl("unitcost") as RadTextBox;
if (txtQty != null && txtAmount != null && txtUnitCost != null)
{
txtQty.Attributes.Add("onBlur", "calculate('" + txtQty.ClientID + "','" + txtAmount.ClientID + "','" + txtUnitCost.ClientID + "')");
}
}
}
使用Javascript:
<script type="text/javascript">
function calculate(price, quantity, totalAmount) //this method throws error
{
//var text1 = $find(price);
//var text2 = $find(quantity);
//var text3 = $find(totalAmount);
//var total = text1.GetValue() * text2.GetValue();
//text3.SetValue(total);
//commented out the above lines because .GetValue() and .SetValue() throws an error "Object does not support this method"
var txtqty = $find(qtyCtrl);
var txtunitcost = $find(unitcostCtrl);
var txtvalued = $find(valuedCtrl);
var qty = txtqty.get_displayValue();
var unitcost = txtunitcost.get_displayValue();
var amount = qty * unitcost;
txtvalued.set_displayValue(amount);
}
</script>