302班

          java突擊隊
          posts - 151, comments - 74, trackbacks - 0, articles - 14
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          日歷

          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          搜索

          •  

          積分與排名

          • 積分 - 154334
          • 排名 - 394

          最新評論

          GridView自定義分頁導航

          Posted on 2007-07-13 18:43 停留的風 閱讀(836) 評論(0)  編輯  收藏 所屬分類: .NET技巧特輯

          自己做的一個項目中所運用到的技術:

          |1.         日歷控件(帶時分秒)
          2.        
          GridView 批量刪除,自定義分頁,定位頁碼
          3.         GridView 修改
          4.        GridView 鼠標經過改變行的顏色
          效果如下:

          HTML:
          <%@ Page Language="C#" AutoEventWireup="true" CodeFile="StfCmpManager.aspx.cs" Inherits="StfCmpManager" %>

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

          <html xmlns="http://www.w3.org/1999/xhtml" >
          <head id="Head1" runat="server">
              
          <title>合作商信息維護</title>
              
          <script language="javascript" type="text/javascript">    
              
          // 判斷多選是否與選中項(沒有選中的返回false)
              function slcNo_click()
              
          {
                  
          if (document.form1.checkboxname.length)
                  
          {
                      
          for (var i=0;i<document.form1.checkboxname.length;i++)
                      
          {
                          
          if(document.form1.checkboxname[i].checked)
                          
          {
                              
          return true;
                          }

                      }

                  }

                  
          else
                  
          {
                      
          if(document.form1.checkboxname.checked)
                      
          {
                          
          return true;
                      }

                  }

                  alert(
          "請選擇后再操作!");
                  
          return false;
              }

              
              
          // 鼠標經過改變行的顏色
              if (!objbeforeItem)
              
          {
                  var objbeforeItem
          =null;
                  var objbeforeItembackgroundColor
          =null;
              }
              
              function ItemOver(obj)
              
          {
                  
          if(objbeforeItem)
                  
          {
                      objbeforeItem.style.backgroundColor 
          = objbeforeItembackgroundColor;
                  }

                  objbeforeItembackgroundColor 
          = obj.style.backgroundColor;
                  objbeforeItem 
          = obj;
                  obj.style.backgroundColor 
          = "#B9D1F3";     
              }

              
          //        
           
              
          // 多選的全選與取消
              function checkJs(boolvalue)
              
          {
                  
          if(document.all.checkboxname.length>1)
                  
          {
                      
          for(var i=0;i<document.all.checkboxname.length;i++)
                      
          {
                          document.all.checkboxname[i].
          checked = boolvalue;            
                      }

                  }

                  
          else
                      document.all.checkboxname.
          checked = boolvalue;
              }

              
          //
              
              
          // 只有全部選中時“全選”選中
              function SingleCheckJs()
              
          {
                  var flag1
          =false;
                  var flag2
          =false;
                  
                  
          if (document.form1.checkboxname.length)
                  
          {
                      
          for (var i=0;i<document.form1.checkboxname.length;i++)
                      
          {
                          
          if(document.form1.checkboxname[i].checked)
                              flag1 
          = true;
                          
          else
                              flag2 
          = true;
                      }

                  }

                  
          else
                  
          {
                      
          if(document.form1.checkboxname.checked)
                          flag1 
          = true;
                      
          else
                          flag2 
          = true;
                  }

                  
                  
          if(flag1==true&&flag2==false)
                      document.getElementById(
          "chk").checked = true;
                  
          else
                      document.getElementById(
          "chk").checked = false;
              }

             
              
          </script>  
          <script type="text/javascript" language="javascript" src="Selecttime/calendar.js"></script>      
          </head>
          <body>
              
          <form id="form1" runat="server">
                      
          <table id="Table10" cellSpacing="0" cellPadding="0" border="0" Style="z-index: 103;
                      left: 21px; position: absolute; top: 20px; width: 90%;">
                          <tr>
                              
          <td bgColor="#fafafa">
                                  
          <FIELDSET style="BORDER-RIGHT: #cccccc 1px solid; BORDER-TOP: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; BORDER-BOTTOM: #cccccc 1px solid; HEIGHT: 45px"><LEGEND><FONT face="宋體" size="2">合作商查詢</FONT></LEGEND>
                                      
          <table id="Table1" height="40" cellSpacing="0" cellPadding="0" width="100%" align="center"
                                          border
          ="0">
                                          
          <tr>
                                              
          <td height="30"><FONT face="宋體">&nbsp;</FONT><FONT face="宋體">&nbsp;合作商名稱
                                                      
          <asp:textbox id="TStfName" runat="server" BorderStyle="Groove" Width="144px"></asp:textbox>&nbsp;</FONT></td>
                                          
          </tr>
                                          
          <tr>
                                              
          <td style="height: 30px">&nbsp;<FONT face="宋體"> 添加時間 </FONT>
                                                  
          <asp:textbox id="StartTime" onfocus="setday(this)" runat="server" Width="159px"></asp:textbox>&nbsp;到
                                                  
          <asp:textbox id="EndTime" onfocus="setday(this)" runat="server" Width="160px"></asp:textbox>&nbsp;&nbsp;&nbsp;<FONT face="Times New Roman">&nbsp;&nbsp;&nbsp;</FONT></td>
                                          
          </tr>
                                          
          <TR>
                                              
          <TD height="30"><FONT face="宋體">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT face="宋體">
                                                          
          <asp:button id="SelectClient" runat="server" Text="搜 索" onclick="SelectClient_Click"></asp:button>&nbsp;&nbsp;&nbsp;</FONT></FONT></TD>
                                          
          </TR>
                                      
          </table>
                                  
          </FIELDSET>
                              
          </td>
                          
          </tr>
                      
          </table>
                      
          <table cellpadding="0" cellspacing="0" border="0" Style="z-index: 103;
                      left: 21px; position: absolute; top: 140px; width: 90%;">
                          <tr bgColor="#6B696B">
                              
          <td align="center" style="width: 85%">
                                  
          <asp:GridView ID="GridView1" runat="server" Width="100%" CellPadding="4" ForeColor="#333333"
                                      AutoGenerateColumns
          ="False" AllowPaging="True" BorderColor="Silver"
                                      BorderStyle
          ="Solid" BorderWidth="1px" OnRowDataBound="GridView1_RowDataBound"
                                      ShowFooter
          ="false" EmptyDataText="沒有數據記錄!!" AllowSorting="True" OnSorting="GridView1_Sorting">
                                      
          <Columns>
                                          
          <asp:BoundField HeaderText="編號" DataField="id" Visible="False" />
                                          
          <asp:TemplateField HeaderText="&lt;input type='checkbox' id='chk' name='chk' onclick='checkJs(this.checked);'  /&gt;全選">
                                              
          <ItemTemplate>
                                                  
          <input type="checkbox" id="checkboxname" name="checkboxname" value='<%# DataBinder.Eval(Container.DataItem, "StfId")%>' onclick='SingleCheckJs();' />
                                              
          </ItemTemplate>
                                          
          </asp:TemplateField>

                                          
          <asp:TemplateField HeaderText="序號">
                                              
          <ItemTemplate>
                                                  
          <%# (Container.DataItemIndex+1).ToString()%>
                                              
          </ItemTemplate>
                                          
          </asp:TemplateField>
                                          
          <asp:TemplateField HeaderText="充值">
                                              
          <ItemTemplate>
                                                  
          <%#GetClientVisitInfoShowCmpMoney(DataBinder.Eval(Container.DataItem,"StfId"),DataBinder.Eval(Container.DataItem,"StfName"))%>
                                              
          </ItemTemplate>
                                          
          </asp:TemplateField>                                                       
                                          
          <asp:BoundField HeaderText="公司名稱" DataField="StfCmpName" SortExpression="StfCmpName"/>
                                          
          <asp:BoundField HeaderText="登陸名" DataField="StfName" SortExpression="StfName" />
                                          
          <asp:BoundField HeaderText="帳戶金額" DataField="StfCmpMoney" SortExpression="StfCmpMoney" />
                                          
          <asp:BoundField HeaderText="帳戶Q幣" DataField ="StfQbmoney" SortExpression="StfQbmoney" />
                                          
          <asp:BoundField HeaderText="合作日期" DataField="StfRegisterTime" HtmlEncode="False" SortExpression="StfRegisterTime" />
                                          
          <asp:TemplateField HeaderText="操作" >
                                              
          <ItemTemplate>
                                                  
          <%#GetClientVisitInfoShowAnchorStr(DataBinder.Eval(Container.DataItem,"StfId"),DataBinder.Eval(Container.DataItem,"StfName"))%>
                                              
          </ItemTemplate>
                                          
          </asp:TemplateField>
                                      
          </Columns>
                                      
          <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                      
          <EditRowStyle BackColor="#999999" />
                                      
          <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                      
          <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                      
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                      
          <PagerSettings Visible="False" />
                                      
          <HeaderStyle Font-Bold="False" Font-Italic="False" />
                                  
          </asp:GridView>
                              
          </td>
                          
          </tr>
                          
          <tr>
                              
          <td align="center" style="height: 25px; width: 569px;">
                                  
          <asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
                                      runat
          ="server">首 頁</asp:LinkButton>&nbsp;<asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一頁</asp:LinkButton>
                                  
          <asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 頁</asp:LinkButton>
                                  
          <asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
                                  
          <asp:Label ID="LblPageCount" runat="server"></asp:Label>
                                  
          <asp:Label ID="LblRecordCount" runat="server"></asp:Label>
          跳轉到第
          <asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
          </asp:DropDownList></td>                <tr>
                              
          <td style="width: 569px">
                                  
          &nbsp;操作:
                                  
          <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="刪除合作商" />
                              
          </td>
                          
          </tr>
                      
          </table>            
                 
          </form>
          </body>
          </html>
          CS:
          using System;
          using System.Data;
          using System.Configuration;
          using System.Collections;
          using System.Web;
          using System.Web.Security;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.Web.UI.WebControls.WebParts;
          using System.Web.UI.HtmlControls;
          using System.Data.SqlClient;

          public partial class StfCmpManager : System.Web.UI.Page
          {
              
          protected void Page_Load(object sender, System.EventArgs e)
              
          {
                 
                  
          string StfRight = "";
                  
          // 在此處放置用戶代碼以初始化頁面
                  if (Session["UserId"== null)
                  
          {
                      Response.Write(
          "<script language='javascript'>alert('工作超時,請重新登錄。');top.window.location='default.aspx';</script>");
                      
          return;
                  }

                  
          else
                  
          {
                      StfRight 
          = Session["UserRight"].ToString().Trim();

                      
          if (!this.IsPostBack)
                      
          {
           
                          
          if (StfRight != "7" && StfRight != "8")
                          
          {
                              Response.Write(
          "<script language='javascript'>alert('你沒有權限察看此頁。');top.window.location='default.aspx';</script>");
                              
          return;
                          }


                          GridViewBind(
          "");
                      }

                  }


              }

              
          private void GridViewBind(string Sqlsort)
              
          {
                  
          string where = string.Empty;
                  
          where = "and a.StfRight='6' and a.StfId=b.Stfid";
                  
          if (TStfName.Text.Trim() != "")
                  
          {
                      
          where += " and a.StfCmpName like '%" + TStfName.Text.Trim() + "%'";
                  }



                  
          if (StartTime.Text.Trim() != "")
                  
          {
                      
          where += " and a.StfRegistertime >= '" + StartTime.Text + "'";
                  }

                  
          if (EndTime.Text.Trim() != "")
                  
          {
                      
          where += " and a.StfRegistertime <= '" + EndTime.Text + "'";
                  }


                  
          if (where != string.Empty)
                  
          {
                      
          where = "Where " + where.Substring(4);
                  }

                  
          string connStr = ConfigurationManager.AppSettings.Get("DataConnectionString");
                  
          string SqlStr = "Select a.StfId as StfId,a.StfName as StfName,a.StfRealName as StfRealName,a.StfCmpName as StfCmpName,b.StfCmpMoney as StfCmpMoney,b.StfQbmoney as StfQbmoney,a.StfRegistertime as StfRegistertime From TStafferInfo a,TCmpMoney b " + where + Sqlsort;
                  
          //Response.Write(SqlStr);
                  
          //Response.End();       
                  DataSet ds = new DataSet();

                  
          try
                  
          {
                      SqlConnection conn 
          = new SqlConnection(connStr);
                      
          if (conn.State.ToString() == "Closed") conn.Open();

                      SqlDataAdapter da 
          = new SqlDataAdapter(SqlStr, conn);
                      da.Fill(ds, 
          "TStafferInfo");
                      
          if (conn.State.ToString() == "Open") conn.Close();

                      GridView1.DataSource 
          = ds.Tables[0].DefaultView;
                      GridView1.DataBind();

                      LblCurrentIndex.Text 
          = "第 " + (GridView1.PageIndex + 1).ToString() + " 頁";
                      LblPageCount.Text 
          = "共 " + GridView1.PageCount.ToString() + " 頁";
                      LblRecordCount.Text 
          = "總共 " + ds.Tables[0].Rows.Count.ToString() + " 條";
                      
          if (ds.Tables[0].Rows.Count == 0)
                      
          {
                          btnFirst.Visible 
          = false;
                          btnPrev.Visible 
          = false;
                          btnNext.Visible 
          = false;
                          btnLast.Visible 
          = false;

                          LblCurrentIndex.Visible 
          = false;
                          LblPageCount.Visible 
          = false;
                          LblRecordCount.Visible 
          = false;
                      }

                      
          else if (GridView1.PageCount == 1)
                      
          {
                          btnFirst.Visible 
          = false;
                          btnPrev.Visible 
          = false;
                          btnNext.Visible 
          = false;
                          btnLast.Visible 
          = false;
                      }


                      
          // 計算生成分頁頁碼,分別為:"首 頁" "上一頁" "下一頁" "尾 頁"
                      btnFirst.CommandName = "1";
                      btnPrev.CommandName 
          = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());

                      btnNext.CommandName 
          = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
                      btnLast.CommandName 
          = GridView1.PageCount.ToString();
                      
          //

                      
          this.ddlCurrentPage.Items.Clear();
                      
          for (int i = 1; i <= this.GridView1.PageCount; i++)
                      
          {
                          
          this.ddlCurrentPage.Items.Add(i.ToString());
                      }

                      
          this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex; 


                  }

                  
          catch (Exception ex)
                  
          {
                      Response.Write(
          "數據庫錯誤,錯誤原因:" + ex.Message);
                      Response.End();
                  }

              }

              
          protected void PagerButtonClick(object sender, EventArgs e)
              
          {
                  GridView1.PageIndex 
          = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
                  GridViewBind(
          "");
              }


              
          protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
              
          {
                  e.Row.Attributes[
          "onmouseover"= "ItemOver(this)";
              }



              
          protected void Button1_Click(object sender, EventArgs e)
              
          {
                  Response.Write(Request.Form.Get(
          "RadioName"));
              }

              
          protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
              
          {
                  
          string sql = "";

                  
          if (ViewState["DataConnectionString"== null || ViewState["DataConnectionString"].ToString().CompareTo(""== 0)
                  
          {
                      ViewState[
          "DataConnectionString"= " desc";
                  }

                  
          else
                      ViewState[
          "DataConnectionString"= "";

                  sql 
          = " order by " + e.SortExpression + ViewState["DataConnectionString"];
                  GridViewBind(sql);

                  
          //DataFormatString="{0:yyyy年MM月dd日 hh時mm分ss秒}"
              }

              
              
          protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
              
          {
                  
          this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
                  GridViewBind(
          "");
              }


              
          private bool CheckAddClientInfoForm()
              
          {
                  
          int Tag = 0;



                  
          if (Tag == 1)
                  
          {

                      Response.Write(
          "<script>alert('請將帶*項填寫!')</script>");
                      
          return false;
                  }

                  
          if (Tag == 2)
                  
          {

                      Response.Write(
          "<script>alert('兩次輸入密碼不同')</script>");
                      
          return false;
                  }

                  
          else
                  
          {
                      
          return true;
                  }

              }


              
          public string GetClientVisitInfoShowAnchorStr(object StfId, object StfName)
              
          {
                  
          string AnchorStr = "<a href='#'><Div onclick='javascript:window.open("TManageModify.aspx?StfId=" + StfId + "&StfName=" + StfName + "","","height=280,Width=600,scrollbars=yes,resizable=yes,status=no")'>修改</Div></a>";
                  
          return AnchorStr;
              }

              
          public string GetClientVisitInfoShowCmpMoney(object StfId, object StfName)
              
          {
                  
          string AnchorStr = "<a href='#'><Div onclick='javascript:window.open("StfCmpMoneyInfo.aspx?StfId=" + StfId + "&StfName=" + StfName + "","","height=250,Width=500,scrollbars=yes,resizable=yes,status=no")'>充值</Div></a>";
                  
          return AnchorStr;
              }

              
          protected void Button3_Click(object sender, EventArgs e)
              
          {
                  
          string str = "";
                  
          string[] ckb = null;

                  str 
          = Request.Form.Get("checkboxname");
                  ckb 
          = str.Split(new char[] ',' });
                  
          for (int i = 0; i < ckb.Length; i++)
                  
          {   //帳戶和信息同時刪除
                      string StrSql = " Delete TStafferInfo where StfId ='" + ckb[i] + "'";
                      String StrSql_d 
          = " Delete TCmpMoneyInfo where StfId ='" + ckb[i] + "'";
                      SqlConnection Conn 
          = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("DataConnectionString"));
                      SqlCommand Cmd 
          = new System.Data.SqlClient.SqlCommand(StrSql, Conn);
                      SqlCommand Cmd_d 
          = new System.Data.SqlClient.SqlCommand(StrSql_d, Conn);

                      Conn.Open();
                      SqlTransaction Trans 
          = Conn.BeginTransaction();
                      
          try
                      
          {
                          Cmd.Transaction 
          = Trans;
                          Cmd_d.Transaction 
          = Trans;
                          Cmd.ExecuteNonQuery();
                          Cmd_d.ExecuteNonQuery();
                          Trans.Commit();

                      }

                      
          catch (Exception Err)
                      
          {
                          Trans.Rollback();
                          Response.Write(Err.Message);
                      }


                  }

                  Response.Write(
          "<script>window.close();alert('操作員刪除成功!');</script>");
                  GridViewBind(
          "");
              }


              
          protected void SelectClient_Click(object sender, EventArgs e)
              
          {
                  GridViewBind(
          "");
              }

           
          }

           

          主站蜘蛛池模板: 云梦县| 张家口市| 枝江市| 浦东新区| 东辽县| 宁都县| 陇川县| 大田县| 清苑县| 盖州市| 莱州市| 漳州市| 长汀县| 东阳市| 探索| 商洛市| 林州市| 唐河县| 丹阳市| 大同县| 刚察县| 临夏市| 桓台县| 抚远县| 镇巴县| 漳平市| 达日县| 界首市| 谢通门县| 衢州市| 枣阳市| 会东县| 同仁县| 通道| 盖州市| 东辽县| 山阴县| 扎赉特旗| 苍南县| 曲阜市| 济阳县|