【问题标题】:template field issue in the gridview网格视图中的模板字段问题
【发布时间】:2011-07-21 08:33:09
【问题描述】:

问:

我有以下问题,我不知道如何真正解决它。

我有一个grid view,其中一列是template field(文本框)。网格视图由 8 rows 组成。我所做的是每次用户在文本框中输入数据时,我将总数放在最后一个文本框中(设置启用 = false)。我通过某种方法对文本框中的数据输入求和并在事件中调用它text changed 。但是每次我在文本框中输入一个数字然后单击Tab in the keyboard 或使用鼠标光标移动到下一个框时,我都会失去焦点,我必须再次将鼠标光标放在预期的文本框中。

我尝试了以下方法来解决我的问题,但都是徒劳的。

 foreach (GridViewRow r in gv_Evaluation.Rows)
            {
                ((RadTextBox)r.Cells[3].FindControl("txt_evaluateWeights")).Attributes.Add("blur", "calc()");
            }

在我的页面加载中,这根本不起作用。


protected void txt_evaluateWeights_TextChanged(object sender, EventArgs e)
{
    calc();
    ((TextBox)sender).Focus();
}

这样将焦点返回到上一个文本框(我的意思是我已经完成的文本框)而不是我想要焦点的文本框,以输入数据。

编辑:

我的计算方法:

private void calc()
        {
            float sum = 0;
            for (int i = 0; i < 7; i++)
            {
                RadTextBox txt1 = (RadTextBox)gv_Evaluation.Rows[i].Cells[3].FindControl("txt_evaluateWeights");
                int weight;
                bool result = Int32.TryParse(txt1.Text, out weight);
                if (result)
                {
                    sum += weight;
                }
            }

            double percentage;
            percentage = Math.Round((sum / 100) * 100, 2);
            RadTextBox txt3 = (RadTextBox)gv_Evaluation.Rows[7].Cells[3].FindControl("txt_evaluateWeights");
            txt3.Text = percentage.ToString();//string.Format("{0:0.0%}", percentage.ToString());

        }

【问题讨论】:

  • 你能把你的calc函数也贴出来吗?
  • 您想在服务器端回发并计算总和还是在客户端进行?另外,您是否考虑过启用分页并且客户端根本不存在某些行时的问题? “将总数放在最后一个文本框中”,行中的最后一个文本框或 f.e. 是什么意思?在页脚中?
  • 首先:gridview 在update panel 中。其次:我想在server side 上进行计算,因为我将它们存储在我的数据库中。第三:no paging,只有八行,因为,我是构建作为数据源的数据表的人。第四:row中的最后一个文本框数字八不在页脚中。
  • 你为什么要把你的 cal 分配到第八行?
  • @just_name:我不确定我是否理解您的意思:但是每次我在文本框中输入一个数字,然后单击键盘上的 Tab 或使用鼠标光标移动到下一个框我失去了焦点,我必须再次将鼠标光标放在预期的文本框中。 :)

标签: .net asp.net ajax gridview templatefield


【解决方案1】:

使用服务器端 PostBack 执行此操作是一种可怕的方式。

改用 JavaScript。这是jQuery中的一个小例子

网格视图

<asp:GridView ID="DemoGrid" runat="server"
            AutoGenerateColumns="false"
            ShowFooter="true">
    <Columns>
        <asp:TemplateField HeaderText="index">
            <ItemTemplate><%# Container.DataItemIndex + 1 %></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Item">
            <ItemTemplate>
                <asp:Label ID="DemoLabel" runat="server" Text='<%# Container.DataItem %>' />
            </ItemTemplate>
            <FooterTemplate>Total</FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Amount">
            <ItemTemplate>
                <asp:TextBox ID="DemoText" runat="server" CssClass="quantity">
                </asp:TextBox>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="TotalLabel" runat="server" CssClass="result"/>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

背后的代码

protected void Page_Load(object sender, EventArgs e){
    if (!IsPostBack)
    {
        string[] array = new string[] { "demo1", "demo2", "demo3", "demo4", "demo5" };
        DemoGrid.DataSource = array;
        DemoGrid.DataBind();
    }
}

JavaScript (jQuery)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".quantity").bind("blur", function () {
            var $quantity = $(this);
            var quantity = +$quantity.val(); //cast to number
            if (!isNaN(quantity)) {
                var $sum = $quantity.closest("table").find("tr:last .result");
                var sum = +$sum.html();
                $sum.html(sum + quantity);
            }
        });
    });
</script>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多