weixin_33691700 2008-09-29 03:01 采纳率: 0%
浏览 48

在ASP.NET中总计GridView

In one of my ASP.NET Web Applications, I am using a BulkEditGridView (a GridView which allows all rows to be edited at the same time) to implement an order form. In my grid, I have a column which calculates the total for each item (cost x quantity) and a grand total field at the bottom of the page. Currently, however, these fields are only refreshed on every post-back. I need to have these fields updated dynamically so that as users change quantities, the totals and grand total update to reflect the new values. I have attempted to use AJAX solutions to accomplish this, but the asynchronous post-backs interfere with the focus on the page. I imagine that a purely client-side solution exists, and I'm hopeful that someone in the community can share.

  • 写回答

2条回答 默认 最新

  • weixin_33701617 2008-09-29 05:21
    关注

    One solution is to build some javascript in you RowDataBound method to constantly update those totals when the textboxes change.

    So during the RowDataBound, start building a javascript string in memory that will add up the textboxes you need added. What's nice in RowDataBound is you can get the Client Side id's of these textboxes by calling TextBox.ClientId. Add this javascript to the page, then also add an onkeyup event to each textbox you require to call this script.

    So something like (this is a row bound event from a gridview)

    private string _jscript;
    protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
          //Get your textbox
          Textbox tb = e.Row.FindControl("tbAddUp");
          //Add the event that you're going to call to this textbox's attributes
          tb.Attributes.Add("onkeyup", "MyAddUpJavaScriptMethod();");
          //Build the javascript for the MyAddUpJavaScriptMethod
          jscript += "document.getElementById('" + tb.ClientId + '").value + ";
       }
    }
    

    Then once you've built that whole script, use your Page.ClientScript class to add a method to you page which will be called by your onkeyup in your textboxes "MyAddUpJavaScriptMethod"

    Hope that makes sense and helps

    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题