【问题标题】:Gridview calculate columns on runtimeGridview 在运行时计算列
【发布时间】:2011-12-23 06:01:15
【问题描述】:

我有一个三列五行的网格视图,其中包含文本项项模板字段,用于在运行时使用 Gridview 将数据插入数据库。我想使用此网格视图进行一些计算,例如在其中添加 Col1 和 Col2 的列第 3 列以及页脚中 Col1 的总和。我还想在没有回发或页面刷新的情况下在文本框中进行文本更改时进行计算。请告诉我该怎么做。 我有以下代码来制作网格视图

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True">
            <Columns>
                <asp:TemplateField HeaderText="One">
                    <FooterTemplate>
                        <asp:Label ID="lblOne_tot" runat="server"></asp:Label>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Two">
                    <FooterTemplate>
                        <asp:Label ID="lblTwo_tot" runat="server"></asp:Label>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total">
                    <ItemTemplate>
                        <asp:Label ID="lblTotal" runat="server"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

【问题讨论】:

    标签: asp.net gridview


    【解决方案1】:

    在每个调用 JavaScript 函数的文本框上添加一个“onChange”事件。在此函数中,进行计算并更新正确的标签/div/span/等。

    <script language="javascript">
    function doCalculations1(whichControl)
    {
       //do calculations on whichControl.value
       var outputValue = whichControl.value * 5;
       //replace lblOne_tot with clientID after page is built
       document.getElementById("lblOne_tot").value = outputValue;
    }
    </script>
    
    <asp:TextBox id="TextBox1" runat="server" onchange="javascript:doCalculations1(this);" />
    

    【讨论】:

    • 但在 javascript 中,我如何从 gridview 控件中找到文本框,任何 gridview 或建议示例
    • 这篇文章有一个很好的例子:http://stackoverflow.com/a/1129062/743633 您可以创建一个数组来存储第 1、2、3 列等中文本框的所有值。另一种方式,它有效(但我不建议您看到表单发生变化)只是加载表单,获取 ID(例如 GridView1$ctl02$TextBox1)并循环遍历这些值。下一行是 GridView1$ctl03$TextBox1,下一行是 GridView1$ctl04$TextBox1,等等。
    猜你喜欢
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    相关资源
    最近更新 更多