如何将GridView列与文本框相乘

时间:2013-09-25 14:36:46

标签: c# asp.net .net

我有一些带有一些数据的GridView:

<asp:GridView ID="gvSelected" runat="server" 
AutoGenerateColumns = "False" Font-Names = "Arial" 
EmptyDataText = "No Records Selected">
<Columns>
  <asp:BoundField DataField = "ID" HeaderText = "ID" />
  <asp:BoundField DataField = "Name" HeaderText = "Name" />
  <asp:BoundField DataField = "Desc" HeaderText = "Description" />
  <asp:BoundField DataField = "Price" HeaderText = "Price" />
  <asp:TemplateField>
<ItemTemplate>
   <asp:TextBox ID="txtAmount" runat="server" Text="0"
               OnTextChanged="txtAmount_TextChanged" AutoPostBack="true">
  </asp:TextBox> 
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
   <asp:Label ID="lblResult" Text="0"  runat="server" /> 
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Green"></HeaderStyle>
</asp:GridView>

想要将“价格”与TextBox中的某些金额相乘,结果将显示在带有标签(lblResult)的列中。我现在尝试使用txtAmount_TextChanged代码,之前有很多变化,但到目前为止没有结果......有什么建议!?

protected void txtAmount_TextChanged(object sender, EventArgs e)  
    {
          TextBox amount= (TextBox)gvSelected.FindControl("txtAmount");
          Label result = (Label)gvSelected.FindControl("lblResult");
          score.Text = ((Convert.ToInt32(row)) * Convert.ToInt32(amount.Text)).ToString();
     }

1 个答案:

答案 0 :(得分:1)

我昨天做过这个。您可以根据您的要求进行修改

ASPX:

<asp:GridView ID="Gridview1" runat="server" ShowFooter="True" AutoGenerateColumns="False" onrowdatabound="Gridview1_RowDataBound"
    ShowHeaderWhenEmpty="true" EmptyDataText="" CssClass="tabledata" Width="100%">
    <Columns>
        <asp:BoundField DataField="ID" HeaderText="Row Number" />
        <asp:TemplateField HeaderText="KMS Travelled">
            <ItemTemplate>
                <asp:TextBox ID="txtkms" runat="server" Width="50px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rate/KM">
            <ItemTemplate>
                <asp:TextBox ID="txtrateperkm" runat="server" Width="40px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Bill Amt">
            <ItemTemplate>
                <asp:TextBox ID="billamt" runat="server" 
                    Width="60px"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

CS:

protected void Page_Load(object sender, EventArgs e)
{
Employee emp = null;
List<Employee> listEmployee = new List<Employee>();
for (int i = 0; i < 1; i++)
{
    emp = new Employee();
    emp.ID = i;
    //emp.Age = "Age :" + i.ToString();
    //emp.Location = "Location :" + i.ToString();
    listEmployee.Add(emp);
}
Gridview1.DataSource = listEmployee;
Gridview1.DataBind();

 }

  protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
if (e.Row.RowType == DataControlRowType.DataRow)
{
    var txtkms = e.Row.FindControl("txtkms") as TextBox;
    var txtrateperkm = e.Row.FindControl("txtrateperkm") as TextBox;
    var billamt = e.Row.FindControl("billamt") as TextBox;
    var jsFunction = String.Format("CalculateBillAmount('{0}','{1}','{2}');", txtkms.ClientID, txtrateperkm.ClientID, billamt.ClientID);
    txtkms.Attributes.Add("onkeyup", jsFunction);
    txtkms.Attributes.Add("onblur", jsFunction);
    txtrateperkm.Attributes.Add("onkeyup", jsFunction);
    txtrateperkm.Attributes.Add("onblur", jsFunction);
}
}
 class Employee
 {
 public int ID { get; set; }
 }

Javascript:

function CalculateBillAmount(kmID, rateID, amtID) {
var objKm = document.getElementById(kmID), objRate = document.getElementById(rateID), objAmt = document.getElementById(amtID);
objAmt.value = +objKm.value * +objRate.value;
}

希望这会对你有所帮助。请更新以获得进一步的帮助。