实现repeater不刷新批量计算各列之间乘积

测试表结构:

_________________

CREATE TABLE [money] (
 [id] [int] IDENTITY (1, 1) NOT NULL ,
 [price] [money] NULL ,
 [amount] [int] NULL ,
 [accumulate] [money] NULL ,
 CONSTRAINT [PK_money] PRIMARY KEY  CLUSTERED
 (
  [id]
 )  ON [PRIMARY]
) ON [PRIMARY]
GO


_________________________

webform5.aspx:

_______________

<%@ Page language="c#" Codebehind="WebForm5.aspx.cs" AutoEventWireup="false" Inherits="abc.WebForm5" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm5</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="100%" border="1">
     <TR>
      <TD style="HEIGHT: 40px" align="center">实现repeater不刷新批量计算各列之间乘积</TD>
     </TR>
     <TR>
      <TD align="center">
        <TABLE id="Table2" cellSpacing="1" cellPadding="1" width="90%" border="1">
        <TR>
         <TD align="center">单价</TD>
         <TD align="center">数量</TD>
         <TD align="center">总计</TD>
        </TR>
        <asp:Repeater ID=list Runat=server>
        <ItemTemplate>
        <TR>
         <TD><asp:Label ID=TextBox1 Runat=server Visible=False Text='<%# DataBinder.Eval(Container.DataItem,"id")%>' /><input type=text id='a<%#DataBinder.Eval(Container.DataItem,"id")%>'  name='a<%#DataBinder.Eval(Container.DataItem,"id")%>'     value='<%# DataBinder.Eval(Container.DataItem,"price")%>' onblur="c<%#DataBinder.Eval(Container.DataItem,"id")%>.value=a<%#DataBinder.Eval(Container.DataItem,"id")%>.value*b<%#DataBinder.Eval(Container.DataItem,"id")%>.value;"></TD>
         <TD><input type=text id='b<%# DataBinder.Eval(Container.DataItem,"id")%>'   name='b<%#DataBinder.Eval(Container.DataItem,"id")%>'   value='<%# DataBinder.Eval(Container.DataItem,"amount")%>'  onblur="c<%#DataBinder.Eval(Container.DataItem,"id")%>.value=a<%#DataBinder.Eval(Container.DataItem,"id")%>.value*b<%#DataBinder.Eval(Container.DataItem,"id")%>.value;"></TD>
         <TD><input type=text id='c<%# DataBinder.Eval(Container.DataItem,"id")%>'      name='c<%#DataBinder.Eval(Container.DataItem,"id")%>'          value='<%# DataBinder.Eval(Container.DataItem,"accumulate")%>'></TD>
        </TR>
        </ItemTemplate>
        </asp:Repeater>
       
       </TABLE>
      
      </TD>
     </TR>
     <TR>
      <TD align="center"><asp:button id="Button1" runat="server" Text="提 交"></asp:button></TD>
     </TR>
     <TR>
      <TD align="center">
       <TABLE id="Table2" cellSpacing="1" cellPadding="1" width="90%" border="1">
        <TR>
         <TD align="center">单价</TD>
         <TD align="center">数量</TD>
         <TD align="center">总计</TD>
        </TR>
        <asp:repeater id="result" Runat="server">
         <ItemTemplate>
          <TR>
           <TD><%# DataBinder.Eval(Container.DataItem,"price")%></TD>
           <TD><%# DataBinder.Eval(Container.DataItem,"amount")%></TD>
           <TD><%# DataBinder.Eval(Container.DataItem,"accumulate")%></TD>
          </TR>
         </ItemTemplate>
        </asp:repeater></TABLE>
      </TD>
     </TR>
    </TABLE>
   </FONT>
  </form>
 </body>
</HTML>

_________________________

webform5.aspx.cs

__________________________-

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace abc
{
 /// <summary>
 /// WebForm5 的摘要说明。
 /// </summary>
 public class WebForm5 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Repeater list;
  protected System.Web.UI.WebControls.Repeater result;
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!Page.IsPostBack)
   {
    using (SqlConnection Conn = new SqlConnection(数据库连接字符串))
    {
      Conn.Open();
     using (SqlCommand Cmd = new SqlCommand())
     {
       Cmd.Connection = Conn;
                       Cmd.CommandText = "select * from money";
      Cmd.CommandType = CommandType.Text;
      SqlDataReader dr = Cmd.ExecuteReader();
                        list.DataSource = dr;
      list.DataBind();
      dr.Close();
      Cmd.Dispose();
     }
     Conn.Dispose();
     Conn.Close();
    }
    
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {

   //把结果存入DATATABLE并显示出来
   DataTable dt = new DataTable();
   
   DataColumn dc = new DataColumn() ;
   
       dc.ColumnName = "price";
       dc.DataType = System.Type.GetType("System.Double");
                                           
       dt.Columns.Add(dc);
   DataColumn dcc = new DataColumn() ;
       dcc.ColumnName = "amount";
       dcc.DataType = System.Type.GetType("System.Double");
       dt.Columns.Add(dcc);
   DataColumn dct = new DataColumn() ;
       dct.ColumnName = "accumulate";
       dct.DataType = System.Type.GetType("System.Double");
       dt.Columns.Add(dct);
      
   int intCount = list.Items.Count;
   for (int i = 0; i<intCount; i++)
   {
      DataRow dr = dt.NewRow();
    string strId = ((Label)list.Items[i].FindControl("TextBox1")).Text;
  
    dr["price"] = Request["a"+strId];
    dr["amount"] = Request["b"+strId];
    dr["accumulate"] = Request["c"+strId];
     
       dt.Rows.Add(dr);
   }
     
                result.DataSource = dt;
       result.DataBind();
   
  }
 }
}

阅读更多
换一批

没有更多推荐了,返回首页