我正在尝试创建一个网格视图,它将从我的数据库表中加载项目名称和项目成本,并有一个模板字段,用户可以输入他们购买的单位数量,最后有最后一个网格视图的列计算总数(单位成本*金额),但我是Web应用程序开发的新手,并且不确定如何使最后一部分发生。
那么,我如何编写网格视图列以将其他两列的模板字段相乘?
修改
好!对不起我之前没有任何信息......我现在有一些代码。我试图添加每行的列而不是总计。这是我到目前为止所拥有的
<asp:GridView ID="gvitemsList" runat="server" AutoGenerateColumns="False" DataSourceID="LinqDataSource1">
<Columns>
<asp:BoundField DataField="itemDescription" HeaderText="Item" ReadOnly="True" SortExpression="itemDescription" />
<asp:TemplateField HeaderText="Quantity"><ItemTemplate><asp:TextBox runat="server" ID="txtQuantity" CssClass="mpprefixtextbox"></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Material"> <ItemTemplate>
<asp:Label ID="lblMat" runat="server" Text='<%#Bind("unitPrice")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OH Amount"><ItemTemplate><asp:Label runat="server" ID="lblOverhead"></asp:Label></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Capital"><ItemTemplate><asp:Label runat="server" ID="lblCapital"></asp:Label></ItemTemplate></asp:TemplateField>
</Columns>
</asp:GridView>
代码隐藏
Private Sub gvitemsList_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles gvitemsList.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim txtQuantity As TextBox = DirectCast(e.Row.FindControl("txtQuantity"), TextBox)
Dim lblMat As Label = DirectCast(e.Row.FindControl("lblMat"), Label)
Dim lblCapital As Label = DirectCast(e.Row.FindControl("lblCapital"), Label)
If (txtQuantity IsNot Nothing) AndAlso (lblMat IsNot Nothing) AndAlso (lblCapital IsNot Nothing) Then
Dim lblMatValue As Integer = txtQuantity.Text + CInt(lblMat.Text)
lblCapital.Text = lblMatValue.ToString()
Dim eventHandler As String = String.Format("textBoxOnBlur('{2}', '{1}', '{4});", txtQuantity.ClientID, lblMat.ClientID, lblCapital.ClientID)
txtQuantity.Attributes.Add("onblur", eventHandler)
End If
End If
End Sub
这是我的javascript
function textBoxOnBlur(txtQuantity, lblMaterials, lblCapital)
{
var textQuantity = document.getElementById(txtQuantity);
var labelMaterials = document.getElementById(lblMat);
var labelCapital = document.getElementById(lblCapital);
var textQuantity = parseInt(textQuantity.value);
var labelMaterials = parseInt(labelMaterials.value);
if ( isNaN(textQuantity) == true )
textQuantity = 0;
labelCapital.innerHTML = textQuantity + labelMaterials;
}
我首先得到一个错误,说无法将字符串转换为双倍...我认为我没想做,我对此非常陌生!