grieview插入文本框自动计算

页面:

 <asp:GridView ID="GridView2" onprerender="GridView2_PreRender" runat="server"  onrowcommand="GridView2_RowCommand"
                                        AutoGenerateColumns="False" BorderColor="Black" BorderWidth="1px" Height="16px"
                                        HorizontalAlign="Left" Width="980px" EnableModelValidation="True"
                                        PageSize="100" onrowdatabound="GridView2_RowDataBound">
                                        <HeaderStyle BackColor="#006699" BorderColor="Black" BorderWidth="1px"
                                            ForeColor="White" HorizontalAlign="Center" Font-Size="10pt" />
                                        <FooterStyle BackColor="#C6C3C6" BorderColor="Black" BorderWidth="1px"
                                            ForeColor="Black" />
                                        <RowStyle BackColor="#FFFFFF" BorderColor="Black" BorderWidth="1px"
                                            ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" />
                                        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                                        <PagerSettings FirstPageText="首页" LastPageText="尾页" NextPageText="下一页"
                                            PreviousPageText="上一页" />
                                        <Columns>
                                            <asp:BoundField DataField="id" HeaderText="ID号" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" Font-Size="1pt" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" Font-Size="1pt"
                                                HorizontalAlign="Center" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="产品种类" HeaderText="产品种类" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="产品名称" HeaderText="产品名称" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="颜色" HeaderText="颜 色" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="长度" HeaderText="长  度" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="单位数量" HeaderText="单位数量" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                                            </asp:BoundField>
                                            <asp:BoundField DataField="单位" HeaderText="单 位" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                                            </asp:BoundField>                              
                      <asp:TemplateField HeaderText="单 价">
                      <ItemTemplate>
                          <asp:TextBox ID="price" runat="server" Style="position: relative" ForeColor="#0000CC"></asp:TextBox>
                      </ItemTemplate>
                          <ControlStyle Width="60px" />                        
                          <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="件 数">
                      <ItemTemplate>
                          <asp:TextBox ID="num" runat="server" Style="position: relative" ForeColor="Red"></asp:TextBox>
                      </ItemTemplate>           
                          <ControlStyle Width="60px" /> <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                      <HeaderStyle BorderWidth="1px" />
                      <ItemStyle BorderWidth="1px" />
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="数 量">
                      <ItemTemplate>
                          <asp:TextBox ID="amt" runat="server" Style="position: relative"></asp:TextBox>
                         
                      </ItemTemplate>
                          <ControlStyle Width="60px" /> <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="金 额">
                      <ItemTemplate>
                          <asp:TextBox ID="jine" Style="position: relative" runat="server" ForeColor="Red"></asp:TextBox>
                                               </ItemTemplate>
                          <ControlStyle Width="70px" /> <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                  </asp:TemplateField>
                                        <asp:ButtonField ButtonType="Button" HeaderText="进仓添加"
                                            Text="添 加" CommandName="添加">
                                            <ControlStyle Width="70px" />
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" ForeColor="#CC0000" />
                                        </asp:ButtonField>
                                            <asp:BoundField DataField="单价" HeaderText="单价" ReadOnly="True">
                                            <HeaderStyle BorderColor="Black" BorderWidth="1px" />
                                            <ItemStyle BorderColor="Black" BorderWidth="1px" HorizontalAlign="Center" />
                                            </asp:BoundField>
               <asp:TemplateField HeaderText="">
                      <ItemTemplate>
                          <asp:TextBox ID="dwsl" runat="server" style="display:none;"></asp:TextBox>
                                               </ItemTemplate>                        
                        <HeaderStyle BorderStyle="None" Width="0px" />
                        <ItemStyle BorderStyle="None" Width="0px" />
                  </asp:TemplateField>
                                           </Columns>
                                    </asp:GridView>

.vb

 Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
            If True Then
            If e.Row.RowType = DataControlRowType.DataRow Then
                Dim tdwsl As TextBox = TryCast(e.Row.Cells(4).FindControl("dwsl"), TextBox)
                Dim tbprice As TextBox = TryCast(e.Row.Cells(0).FindControl("price"), TextBox)
                Dim tbnum As TextBox = TryCast(e.Row.Cells(1).FindControl("num"), TextBox)
                Dim tbamt As TextBox = TryCast(e.Row.Cells(2).FindControl("amt"), TextBox)
                Dim tjine As TextBox = TryCast(e.Row.Cells(3).FindControl("jine"), TextBox)
                If tbprice IsNot Nothing AndAlso tbnum IsNot Nothing AndAlso tbamt IsNot Nothing Then
                    'If Not String.IsNullOrEmpty(tbprice.Text) Then
                    '    tbprice.Text = "0"
                    'End If
                    'If Not String.IsNullOrEmpty(tbnum.Text) Then
                    '    tbnum.Text = "0"
                    'End If
                    'tbamt.Text = tdwsl.ToString
                    'tbamt.Text = (Val(tbnum.Text) * Val(tdwsl)).ToString()
                    'tjine.Text = (Val(tbamt.Text) * Val(tbprice.Text)).ToString()
                    'tbamt.Text = (Decimal.Parse(Decimal.Parse(tbnum.Text) * Decimal.Parse(tbprice.Text))).ToString()
                    Dim script As New StringBuilder()
                    '输入数值之后总自动金额更新
                    script.Append(" var amount=0;")
                    script.Append(" vdwsl=document.getElementById('" + tdwsl.ClientID & "');")
                    script.Append(" vjine=document.getElementById('" + tjine.ClientID & "');")
                    script.Append(" vAmt=document.getElementById('" + tbamt.ClientID & "');")
                    script.Append(" vNum=document.getElementById('" + tbnum.ClientID & "');")
                    script.Append(" vPrice=document.getElementById('" + tbprice.ClientID & "');")
                    'script.Append("var amt=parseFloat(vPrice.value)*parseFloat(vNum.value);")
                    script.Append("var amt=parseFloat(vdwsl.value)*parseFloat(vNum.value);")
                    script.Append("vAmt.value=amt;")
                    script.Append("var jine=parseFloat(vPrice.value*vAmt.value).toFixed(2);")
                    script.Append("vjine.value=jine;")
                    '只允许输入带小数的数字.parseFloat((danjia*shuliang).toFixed(2));
                    Dim scriptOnKeyPress As String = "return   /^[1-9]/d{9}/.test(this.value)?(//./.test(this.value)?(//./d{2}/.test" & vbCr & vbLf & vbCr & vbLf & "(this.value)?(event.keyCode==0):(event.keyCode>=48&&event.keyCode<=57)):(event.keyCode==46)):" & vbCr & vbLf & vbCr & vbLf & "(event.keyCode>=48&&event.keyCode<=57)"
                    tbprice.Attributes.Add("onblur", script.ToString())
                    tbprice.Attributes.Add("onkeypress", scriptOnKeyPress)
                    tbnum.Attributes.Add("onblur", script.ToString())
                    tbnum.Attributes.Add("onkeypress", scriptOnKeyPress)
                    tbamt.Attributes.Add("onblur", script.ToString())
                    tbamt.Attributes.Add("onkeypress", scriptOnKeyPress)
                    'tjine.Attributes.Add("onblur", script.ToString())
                    'tjine.Attributes.Add("onkeypress", scriptOnKeyPress)
                    tdwsl.Attributes.Add("onblur", script.ToString())
                    tdwsl.Attributes.Add("onkeypress", scriptOnKeyPress)
                End If
            End If
        End If

就这么简单.完美运行.QQ71456463

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值