代码改变世界

利用gridview实现计时消费,有点复杂,谁有好的方法可以讨论一下...

2014-02-10 15:05  咩咩羊仔仔  阅读(162)  评论(0编辑  收藏  举报

这是前段时间做项目遇到的一个问题,做出来的效果图如下,

 由会员id查询出会员来,然后开始计费。然后点击结束消费,传到别的页面,主要就是结束时间和开始时间的一个时间差。

用到的数据表设计视图如下,

为了方便研究,我把aspx的代码和cs的代码全部发上来,主要看的地方有,aspx文件的gridview中的TemplateField属性的设置。cs文件下GridView1_RowCreated事件,GridView1_RowCommand事件。

 

aspx代码如下:

  1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="consumeTime.aspx.cs" Inherits="consume_consumeTime" %>
  2 
  3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4 
  5 <html xmlns="http://www.w3.org/1999/xhtml">
  6 <head>
  7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8 <script type="text/javascript" src="../scripts/jquery.min.js"></script>
  9 <script src="../scripts/artDialog4.1.7/artDialog.js?skin=default">function Submit1_onclick() {
 10 
 11                                                                   
 12 }
 13 
 14 </script>
 15 <link type="text/css" rel="stylesheet" href="../inc/skin/default/style.css" />
 16 <title>计时消费</title>
 17 </head>
 18 <script type="text/javascript">
 19   
 20   function calc()
 21 {
 22      //  mysklp= $.ajax({ url: encodeURI("consumeTime_getsklp.aspx? "), async: false }); 
 23        $("#mebrid").attr("value", ""+0+""+0+""+0+""+0+""+0+""+0+""+0+""+0+""+0+""+0+"");
 24        //document.getElementById("shuliang").innerHTML=myshuliang.responseText;
 25        //alert("test");
 26 }
 27  
 28   
 29   </script>
 30 
 31 
 32 <body>
 33 <%--<div class="divContentBox">--%>
 34      
 35   
 36     <form  runat="server">
 37 
 38      
 39   
 40     <div class="divContentHead">
 41       <img alt="" src="../inc/skin/default/images/ico/coins.png" />
 42       <span style="margin-left:2px;">计时消费</span>
 43            
 44             
 45     
 46     </div>
 47     <!--查找-->
 48     <div id="">
 49     
 50       <table class="tableStyle" style="width: 100%">
 51         <tr style="color: #333333; background-color: #F7F6F3;">
 52           <td style="text-align:center;">
 53                <asp:Label ID="Label1" runat="server" Text="会员ID"></asp:Label>
 54                <asp:TextBox ID="mebrid" runat="server" ></asp:TextBox> 
 55                           <label><input type="checkbox" name="sklp" id="sklp" onclick="calc()" />散客令牌</label>&nbsp;
 56             
 57                   <asp:Button ID="Button2" runat="server" Text="开始计费" class="buttonColor" onclick="Button2_Click" 
 58                    />
 59            
 60             
 61     
 62 
 63           </td>
 64         </tr>
 65         
 66       </table>
 67     </div>
 68     <asp:GridView ID="GridView1" runat="server" DataSourceID="AccessDataSource1" 
 69             AllowPaging="True" AutoGenerateColumns="False" 
 70             CellPadding="4" DataKeyNames="ID" ForeColor="#333333" GridLines="None" 
 71             Width="1300px"  AllowSorting="True"   onrowcommand="GridView1_RowCommand" 
 72             onrowcreated="GridView1_RowCreated" 
 73             >
 74         <RowStyle BackColor="#EFF3FB" />
 75         <Columns>
 76             <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
 77                 ReadOnly="True" SortExpression="ID" />
 78             <asp:BoundField DataField="mebrId" HeaderText="会员卡号" 
 79                 SortExpression="mebrId" />
 80             <asp:BoundField DataField="mebrName" HeaderText="会员姓名" 
 81                 SortExpression="mebrName" />
 82             <asp:BoundField DataField="ksTime" HeaderText="开始时间" 
 83                 SortExpression="ksTime" />
 84             <asp:BoundField DataField="jsTime" HeaderText="结束时间" 
 85                 SortExpression="jsTime" />
 86             <asp:BoundField DataField="st" HeaderText="消费状态" SortExpression="st" />
 87             <asp:BoundField DataField="ksCzy" HeaderText="开始操作员" SortExpression="ksCzy" />
 88             <asp:BoundField DataField="jsCzy" HeaderText="结束操作员" SortExpression="jsCzy" />
 89             <asp:TemplateField ShowHeader="False">
 90             
 91                 <ItemTemplate>
 92                     <asp:Button ID="bt" runat="server" CausesValidation="false" CommandName="xfjs" CommandArgument="<%#((GridViewRow)Container).RowIndex%>"                                             Text="消费结束" />
 93                 </ItemTemplate>
 94                 
 95                 
 96             </asp:TemplateField>
 97         </Columns>
 98         <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 99         <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
100         <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
101         <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
102         <EditRowStyle BackColor="#2461BF" />
103         <AlternatingRowStyle BackColor="White" />
104     </asp:GridView>
105   
106     
107 
108     <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/db/mem.mdb" 
109             SelectCommand="SELECT * FROM [jsxf]">
110     </asp:AccessDataSource>
111   
112     
113  </form>
114 
115 </div>
116 </body>
117 </html>
aspx

cs代码如下:

  1 using System;
  2 using System.Collections;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.Security;
  8 using System.Web.UI;
  9 using System.Web.UI.HtmlControls;
 10 using System.Web.UI.WebControls;
 11 using System.Web.UI.WebControls.WebParts;
 12 using System.Xml.Linq;
 13 
 14 public partial class consume_consumeTime : System.Web.UI.Page
 15 {
 16 
 17     public string SQLStrings = "SELECT * FROM [jsxf]   WHERE 1=1";//查找语句 如果为空则忽略此值
 18     public string czy;
 19     public string mid="";
 20     public string memname="" ;
 21     public Common.MBERINFO meminfo;
 22    // public string Sql1, sql1;
 23     //public string b;//一列遍历
 24     public TimeSpan dd;//保存计算时间
 25     public string aa,bb,PostPS,hh ;//会员卡号,消费状态,传送备注,开始时间
 26     public DateTime ksTime, jsTime;//定义开始时间和结束时间
 27     string Dp;
 28 
 29     protected void ShowList(string strSql) //显示列表
 30     {
 31         AccessDataSource1.DataFile = MemData.ClassData.DataFilePath;
 32         AccessDataSource1.SelectCommand = strSql;
 33         GridView1.DataSourceID = "AccessDataSource1";
 34     }
 35     string Czy;
 36     protected void Showlist(string strSQL)//显示列表
 37     {
 38         AccessDataSource1.DataFile = MemData.ClassData.DataFilePath;
 39         AccessDataSource1.SelectCommand = strSQL;
 40         GridView1.DataSourceID = "AccessDataSource1";
 41 
 42     }
 43 
 44     protected void Page_Load(object sender, EventArgs e)
 45     {  
 46 
 47         if (null == Session[Common.ClassCommon.NowAdmin] || "" == Session[Common.ClassCommon.NowAdmin].ToString())
 48         {
 49             Response.Write("<script language='javascript'>alert('请先登录!');</script>");
 50             Response.Write("<script language='javascript'>window.location.href='../Default.aspx';</script>");
 51 
 52         }
 53         else
 54         {
 55             if (MemData.ClassData.IsCzyHasRight("计时消费") == false)
 56             {
 57                 Response.Write("<script language='javascript'>alert('您没有查看此页面的权限!');</script>");
 58                 Response.Write("<script language='javascript'>window.location.href='../startpage.aspx';</script>");
 59             }
 60         }
 61 
 62 
 63         SQLStrings = "SELECT * FROM [jsxf]   WHERE ( 1=1 ";
 64 
 65 
 66         //判断id条件
 67         if (mebrid.Text != "")
 68         {
 69             string idstr = mebrid.Text.ToString();
 70             SQLStrings += "AND [mebrId] = '" + idstr + "'";
 71 
 72         }
 73         SQLStrings += ")order by ID DESC";
 74 
 75         ShowList(SQLStrings);
 76     }
 77 
 78 
 79     //绑定行列号
 80     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
 81     {
 82         if (e.Row.RowType == DataControlRowType.DataRow)
 83         {
 84             Button bt = e.Row.FindControl("bt") as Button;
 85             bt.CommandArgument = e.Row.RowIndex.ToString();
 86 
 87         }
 88     }
 89 
 90     //点击开始消费按钮
 91     protected void Button2_Click(object sender, EventArgs e)
 92 
 93     {
 94         if (null != Request["mebrid"])//获取输入id
 95             mid = Request["mebrid"];
 96 
 97         memname = MemData.ClassData.GetMemName(mid);//获取会员名称
 98         
 99         meminfo=MemData.ClassData.GetMberInfoByID(mid);//有id获取会员信息
100 
101 
102         if (mid != meminfo.MberID)
103         {
104             Response.Write("<script language='javascript'>alert('指定的会员不存在!');history.go(-1);</script>");
105             Response.End();
106         }
107 
108 
109 
110         SQLStrings = "SELECT * FROM [jsxf]    WHERE  ( 1=1 ";
111 
112 
113         //判断id条件
114         if (mebrid.Text != "")
115         {
116             string idstr = mebrid.Text.ToString();
117             SQLStrings += "AND [mebrId] = '" + idstr + "'";
118 
119         }
120         SQLStrings += ")order by ID DESC";
121           
122 
123      
124                if ("" != mid) //如果输入卡号不为空
125                  {
126 
127                         czy = MemData.ClassData.GetNowCzy();
128 
129                         string strSql = "insert into jsxf (mebrId,mebrName,ksTime,jsTime,st,ksCzy,jsCzy)  values ( '" + mid + "','" + memname + "','" + DateTime.Now.ToString() + "','" + DateTime.Today.ToString() + "','" + "正在消费" + "','" + czy + "','" + "" + "' ) ";
130 
131                         int r9 = MemData.ClassData.ExecSql(strSql);
132                         if (-2 == r9 || -3 == r9)
133                             Response.Write("<script language='javascript'>alert('系统异常!');history.go(-1);</script>");
134 
135                     }
136           
137                   ShowList(SQLStrings);//显示列表
138      
139     }
140 
141    
142 
143   
144             protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
145             {
146 
147                 if (null != Request["mebrid"])//获取输入id
148                     mid = Request["mebrid"];
149 
150                 memname = MemData.ClassData.GetMemName(mid);//获取会员名称
151 
152                 meminfo = MemData.ClassData.GetMberInfoByID(mid);//有id获取会员信息
153 
154 
155 
156                if (e.CommandName == "xfjs")//buttonCommandName
157                {
158                    int index = Convert.ToInt32(e.CommandArgument);
159                 
160                    aa = GridView1.Rows[index].Cells[1].Text.ToString();//会员id
161                    bb = GridView1.Rows[index].Cells[6].Text.ToString();//消费状态
162                    
163                    
164 
165                    czy = MemData.ClassData.GetNowCzy();
166 
167                    string Sql1 = "update jsxf set jsTime='" + DateTime.Now.ToString() + "'where  mebrId='" + aa + "' and st = '" + "正在消费" + "'";
168                    string sql1 = "update jsxf set jsCzy='" + czy + "'where mebrId='" + aa + "' and st = '" + "正在消费" + "'";
169                    string sQl1 = "update jsxf set st='" + "结束消费" + "'where mebrId='" + aa + "' and st = '" + "正在消费" + "'";
170 
171                    int r3 = MemData.ClassData.ExecSql(sql1);
172                    if (-2 == r3 || -3 == r3)
173                        Response.Write("<script language='javascript'>alert('系统异常!');history.go(-1);</script>");
174 
175 
176                    int r4 = MemData.ClassData.ExecSql(Sql1);
177                    if (-2 == r4 || -3 == r4)
178                        Response.Write("<script language='javascript'>alert('系统异常!');history.go(-1);</script>");
179 
180                    int r6 = MemData.ClassData.ExecSql(sQl1);
181                    if (-2 == r6 || -3 == r6)
182                        Response.Write("<script language='javascript'>alert('系统异常!');history.go(-1);</script>");
183 
184 
185 
186 
187                    SQLStrings = "SELECT * FROM [jsxf]   WHERE ( 1=1 ";
188 
189 
190                    //判断id条件
191                    if (mebrid.Text != "")
192                    {
193                        string idstr = mebrid.Text.ToString();
194                        SQLStrings += "AND [mebrId] = '" + idstr + "'";
195 
196                    }
197                    SQLStrings += ")order by ID DESC";
198 
199                    ShowList(SQLStrings);
200 
201               
202                    if (bb != "'" + "正在消费" + "'")//判断消费状态
203                    {
204                        
205                        hh = GridView1.Rows[index].Cells[3].Text.ToString();//开始时间
206 
207                        string jstime = DateTime.Now.ToString();//获取当前 结束消费 按下时间
208                        DateTime s2 = Convert.ToDateTime(jstime);
209                        string t1 = hh;
210                        //string t2 = jstime;
211                        
212                        DateTime s1 = DateTime.Parse(t1);
213                       // DateTime s2 = DateTime.Parse(t2);
214                      
215                        dd = s2.Subtract(s1);  //当前时间减去开始时间
216                        
217                        PostPS = "会员ID:" + aa + ",会员姓名:" + memname.ToString() + ",消费时间:" + dd.ToString() + "";//传递PS,传到另一个页面进行计算
218 
219                        Response.Redirect("fastConsume.aspx?PostID="+mid+"&PostPS="+PostPS+"&suc=no");
220                     
221                    }
222                            
223                    
224                    
225                }
226             }
227 
228             //protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
229             //{
230             //    if (e.Row.RowType == DataControlRowType.DataRow)
231             //    {
232             //        Button bt = new Button();
233             //        bt = (Button)e.Row.Cells[1].FindControl("bt");
234             //        bt.CommandArgument = e.Row.RowIndex.ToString();
235 
236 
237 
238             //    }
239 
240             //}
241 
242 
243          }
cs