0

我有一些数据的 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>

想要将“价格”与文本框中的一些金额相乘,结果将显示在带有标签 (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();
     }
4

1 回答 1

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

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

于 2013-09-25T15:15:53.790 回答