javascript来计算gridview中的值

时间:2013-10-30 08:56:51

标签: c# javascript asp.net gridview

我有一个gridview

 <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" CssClass="vutblrow" TabIndex="3" HeaderStyle-CssClass="vutblhdr" CellPadding="4" ForeColor="#333333" GridLines="None"  Width="25%" PagerStyle-Mode="NumericPages"


            AutoGenerateColumns="false" onrowcreated="Gridview1_RowCreated" Height="16px">
              <PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Serial Number" />
            <asp:TemplateField HeaderText="Air/Bus/Train Fare">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" CssClass="txtBoxNormalmedium" ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City To">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" CssClass="txtBoxNormalmedium" ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City From">
                <ItemTemplate>
                     <asp:TextBox ID="TextBox3" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                 <asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
                        onclick="ButtonAdd_Click" CssClass="btnNormalAdd" />
                </FooterTemplate>
            </asp:TemplateField>
                 <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" CssClass="lnkbut">Remove</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            </Columns>
        </asp:gridview>

 <asp:Label ID="lblgpf14" runat="server" CssClass="lblText " Text="Total Air Fare"></asp:Label>      
 <asp:TextBox ID="txt" runat="server" CssClass="txtBoxNormalmedium" Enabled="False" onkeyup="Calculate();"></asp:TextBox>

当用户输入空中/公共汽车/火车票价时,该值应显示在文本框“txt”中(使用javacript中的onkeyup),此文本框位于gridview之外。当用户点击添加新行并输入火车票价的总价值必须显示在“txt”。我真的不知道如何为此写一个javascript。有人请帮帮我吗?

1 个答案:

答案 0 :(得分:0)

将用户输入值的事件绑定(根据您的问题在两个位置),如

<asp:TextBox ID="texboxid" onkeyup="showCal(this)" />

你的js脚本

function showCal(obj){

  var val=document.getElementById(obj.id).value;


      if(val!='' && !isNaN(val) ){
         var prvVal=document.getElementById('txt').value;
         prvVal= (!isNaN(prvVal))?prvVal:0;
        document.getElementById('txt').value = parseInt(prvVal)+parseInt(val);
      }else{
         alert("The value you have entered is either empty or not a number.")
      }
}