在gridview中自动乘以两列

时间:2011-03-26 11:06:59

标签: c# javascript gridview

HI,

我需要在gridview中计算两个textboxex的值,并在第二个文本框中输入值后立即使用javascript在第三个文本框中显示结果。

我的文本框字段是:数量和价格 结果应显示在Total中。

即,(总计)值=数量*费率。

我试过这个, 背后的代码:C#

protected void gvPOItms__RowCreated(Object sender, GridViewRowEventArgs e)
{
    try
    {            
        TextBox txt1 = (TextBox)e.Row.FindControl("txtQty");
        TextBox txt2 = (TextBox)e.Row.FindControl("txtRate");
        TextBox txt3 = (TextBox)e.Row.FindControl("txtValue");

        txt1.Attributes["onKeyup"] = "javascript: return multiplication('" + txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')";
        txt2.Attributes["onKeyup"] = "javascript: return multiplication('" + txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')";
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}

JAVASCRIPT:

<Script type="text/javascript">
 function multiplication(tx1,txt2,txt3)
        {
        //Your logic for multiplication
        var Qty=document.getElementById(txt1).value;
        var Rate=document.getElementById(txt2).value;
        document.getElementById(txt3).value=Qty*Rate;       
        }
    </script> 

标记页:

                                                                                                                                

                                                                                                        

                                                                                                         

但是,我无法得到答案,它没有抛出任何错误。

有人可以告诉我出了什么问题吗?

2 个答案:

答案 0 :(得分:2)

你应该在乘法之前将字符串更改为int(任何文本框的值返回字符串):)

试试这个: -

txt1.Attributes["onKeyup"] = "javascript: return multiplication('" +  Convert.ToInt32(txt1.Text) + "','" +  Convert.ToInt32(txt2.Text) + "','" +  Convert.ToInt32(txt3.ClientID) + "')";
txt2.Attributes["onKeyup"] = "javascript: return multiplication('" +  Convert.ToInt32(txt1.Text) + "','" +  Convert.ToInt32(txt2.Text) + "','" +  Convert.ToInt32(txt3.ClientID) + "')";

在剧本中: -

<Script type="text/javascript">
 function multiplication(Qty,Rate,txt3)
    {
    //Your logic for multiplication

    document.getElementById(txt3).value=Qty*Rate;       
    }
</script> 

答案 1 :(得分:0)

问题在于这些代码行

txt1.Attributes["onKeyup"] = "javascript: return multiplication('" + txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')";
txt2.Attributes["onKeyup"] = "javascript: return multiplication('" + txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')";

使用添加方法

txt1.Attributes.Add("onKeyup", "javascript: return multiplication('" +
     txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')");
txt2.Attributes.Add("onKeyup", "javascript: return multiplication('" +
     txt1.ClientID + "','" + txt2.ClientID + "','" + txt3.ClientID + "')");

并在javascript中执行parseInt()

var Qty  = parseInt(document.getElementById(txt1).value);
var Rate = parseInt(document.getElementById(txt2).value);