Grid View文本框添加了? [英] Grid View text boxes adding ?

查看:69
本文介绍了Grid View文本框添加了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何添加(求和)网格视图文本框.我被设计为一个网格视图,在该网格视图中,单行中有7个文本框,我需要动态计算这些文本框值的总数.意味着一旦我们在任何文本框中输入任何值,该值就会显示在总计"文本框中.


网格视图设计:

Hi,

How to add(sum) grid view text boxes. I was designed one grid view, in that grid view there is 7 text boxes in single row, i need to calculate total of these text box values dynamically. Means once we enter any value in any text box, that value is shown in "total" text box.


Grid View Design:

<asp:GridView ID="gvClient" runat="server" AutoGenerateColumns="False"

          EnableModelValidation="True" BackColor="White" BorderColor="#999999"

          BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"

          HorizontalAlign="Center" Width="100%">
          <AlternatingRowStyle BackColor="Gainsboro" />
          <Columns>
            <asp:TemplateField HeaderText="Client">
              <ItemTemplate>
                <br />
                <asp:DropDownList ID="ddlClient1" runat="server"

                  onselectedindexchanged="ddlClient1_SelectedIndexChanged" AutoPostBack="True">
                </asp:DropDownList>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Billing">
              <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text="Billable"></asp:Label>
                <br />
                <br />
                <asp:Label ID="Label2" runat="server" Text="Non-Billable"></asp:Label>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Sun">
              <ItemTemplate>
                <asp:TextBox ID="txtSun1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtSun1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mon">
              <ItemTemplate>
                <asp:TextBox ID="txtMon1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtMon1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Tue">
              <ItemTemplate>
                <asp:TextBox ID="txtTue1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtTue1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Wed">
              <ItemTemplate>
                <asp:TextBox ID="txtWed1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtWed1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Thu">
              <ItemTemplate>
                <asp:TextBox ID="txtThu1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtThu1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Fri">
              <ItemTemplate>
                <asp:TextBox ID="txtFri1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtFri1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Sat">
              <ItemTemplate>
                <asp:TextBox ID="txtSat1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtSat1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   Width="35px">0</asp:TextBox>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total">
              <ItemTemplate>
                <asp:TextBox ID="txtTot1B" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   ReadOnly="True"

                  Width="35px">0</asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="txtTot1N" runat="server" AutoPostBack="True"

                  CausesValidation="True" Height="18px"   ReadOnly="True"

                  Width="35px">0</asp:TextBox>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Attachments">
              <ItemTemplate>
                <asp:FileUpload ID="fuBill1" runat="server" Height="25px" Width="217px" />
                <asp:Button ID="btnFu1" runat="server" Text="Add" />
                <asp:LinkButton ID="lbBill1" runat="server" Visible="False"></asp:LinkButton>
                <br />
                <br />
                <asp:FileUpload ID="fuNon1" runat="server" Height="23px" Width="216px" />
                <asp:Button ID="btnfu1N" runat="server" Text="Add" />
                <asp:LinkButton ID="lbnon1" runat="server" Visible="False"></asp:LinkButton>
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Status">
              <ItemTemplate>
                <br />
                <asp:DropDownList ID="ddlStatus1" runat="server">
                </asp:DropDownList>
              </ItemTemplate>
            </asp:TemplateField>
          </Columns>
          <FooterStyle BackColor="#063657" ForeColor="Black" Height="25px"

            HorizontalAlign="Center" VerticalAlign="Top" />
          <HeaderStyle BackColor="#063657" ForeColor="White" Height="35px"

            HorizontalAlign="Center" VerticalAlign="Middle" />
          <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
          <RowStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Center"

            VerticalAlign="Middle" />
          <SelectedRowStyle BackColor="#008A8C" ForeColor="White"

            HorizontalAlign="Center" VerticalAlign="Top" />
        </asp:GridView>


I want to add the total of text boxes..with out "Add Button" Click.


I want to add the total of text boxes..with out "Add Button" Click.

推荐答案

Hi,

Instead of achieving this task from server side you can achieve this in javascript. It will be very easier for you and also performance will be very good.

Remove all the AutoPostBack="true" from the textbox. and from RowDataBoundEvent of gridview, Find all the textbox using e.row.FindControl and then use javascript function onblur event of the all textbox. Pass ClientID of in the jsvscript function. get all the textbox value using document.getelementbyID in javascript and then perfom SUM.

示例代码

Hi,

Instead of achieving this task from server side you can achieve this in javascript. It will be very easier for you and also performance will be very good.

Remove all the AutoPostBack="true" from the textbox. and from RowDataBoundEvent of gridview, Find all the textbox using e.row.FindControl and then use javascript function onblur event of the all textbox. Pass ClientID of in the jsvscript function. get all the textbox value using document.getelementbyID in javascript and then perfom SUM.

Sample Code

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Textbox txt1 = ((Textbox)e.Row.FindControl("txt1"));
 Textbox txt2 = ((Textbox)e.Row.FindControl("txt2"));
 Textbox total = ((Textbox)e.Row.FindControl("txt2"));
                txt1.Attributes.Add("onblur","CalculateSum(''"+ txt1.ClientID + "'',''"+ txt2.ClientID +"'',''"+ total.ClientID  +"'');"); 
 txt2.Attributes.Add("onblur","CalculateSum(''"+ txt1.ClientID + "'',''"+ txt2.ClientID +"'',''"+ total.ClientID  +"'');"); 
            }
        }









<script language="javascript">
function CalculateSum(txt1,txt2,total)
{
var txt1 = document.getElementByID(txt1);
var txt2 = document.getElementByID(txt2);
var total = document.getElementByID(total);
total.value = txt1.value + txt2.value;  

}
</script>



You can customize above script according to your need.
Hope this will help :)

谢谢
Vinod



You can customize above script according to your need.
Hope this will help :)

Thanks
Vinod


这篇关于Grid View文本框添加了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆