功能较全的DataGrid分页[收藏]

<%@ Page language="c#" Codebehind="DataGrid2.aspx.cs" AutoEventWireup="false" Inherits="XMLYB.DataGrid2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title></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">
  <LINK href="style/1.css" type="text/css" rel="stylesheet">
  <script language="javascript">
  <!--
  function checkall(all)//用于判断全选记录的函数
   {
    for(i=0;i<Form1.length;i++)
    { 
     document.Form1.elements[i].checked =(all == true)? true:false;   
    } 
    
    var j=0;
    
    for(i=0;i<Form1.length;i++)
    {
     var c= document.Form1.elements[i].checked;
     if(c==true)
      j++;
    }
    if (j>0)  
    {  
     //document.Form1.Del.disabled = false;
    }    
    else    
    {
     //document.Form1.Del.disabled = true;  
    } 
   }
   function check()
   {
    var j=0;
    
    for(i=0;i<Form1.length;i++)
    {
     var c= document.Form1.elements[i].checked;
     if(c==true)
      j++;
    }
    if (j>0)      
    {   
     //document.Form1.Del.disabled = false;     
    }
    else    
    {
     //document.Form1.Del.disabled = true;    
    }
   }
  //-->
  </script>
 </HEAD>
 <body>
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
     <TBODY>
      <TR>
       <TD align="center"><asp:datagrid id="DataGrid1" runat="server" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
         BackColor="White" CellPadding="1" GridLines="Vertical" HorizontalAlign="Center" Width="98%" AutoGenerateColumns="False">
         <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
         <AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
         <ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
         <HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="#000084"></HeaderStyle>
         <FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
         <Columns>
          <asp:TemplateColumn HeaderText="ID">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
           <ItemTemplate>
            <asp:Label id="Label1" runat="server"></asp:Label>
           </ItemTemplate>
          </asp:TemplateColumn>
          <asp:BoundColumn DataField="OrderId" ReadOnly="True" HeaderText="OrderId">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
          </asp:BoundColumn>
          <asp:BoundColumn DataField="CustomerID" ReadOnly="True" HeaderText="CustomerID">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
          </asp:BoundColumn>
          <asp:BoundColumn DataField="ShipName" ReadOnly="True" HeaderText="ShipName"></asp:BoundColumn>
          <asp:BoundColumn DataField="Freight" ReadOnly="True" HeaderText="Freight">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
          </asp:BoundColumn>
          <asp:BoundColumn DataField="ShipCity" ReadOnly="True" HeaderText="ShipCity">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
          </asp:BoundColumn>
          <asp:BoundColumn DataField="ShipCountry" ReadOnly="True" HeaderText="ShipCountry">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
          </asp:BoundColumn>
          <asp:BoundColumn DataField="OrderDate" ReadOnly="True" HeaderText="OrderDate">
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
          </asp:BoundColumn>
          <asp:TemplateColumn>
           <ItemStyle HorizontalAlign="Center"></ItemStyle>
           <HeaderTemplate>
            <INPUT id="chkall" type="checkbox" runat="server" onclick="checkall(this.checked)">
           </HeaderTemplate>
           <ItemTemplate>
            <INPUT id="chkid" type="checkbox" runat="server" onclick="check()">
           </ItemTemplate>
          </asp:TemplateColumn>
         </Columns>
         <PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
        </asp:datagrid></TD>
      </TR>
      <TR height="23">
       <TD align="center">
        <TABLE id="Table2" cellSpacing="0" cellPadding="0" width="98%" align="center" border="0">
         <TBODY>
          <TR>
           <TD align="left" valign="bottom">总共
            <asp:label id="Label2" runat="server" Font-Bold="True" ForeColor="Blue">1</asp:label>&nbsp;条记录
            共
            <asp:label id="Label3" runat="server" Font-Bold="True" ForeColor="Blue">1</asp:label>&nbsp;页
            第
            <asp:label id="Label4" runat="server" Font-Bold="True" ForeColor="Red">1</asp:label>&nbsp;页
            &nbsp;
            <asp:hyperlink id="HyperLink1" runat="server" ToolTip="首页">首页</asp:hyperlink>&nbsp;
            <asp:hyperlink id="HyperLink2" runat="server" ToolTip="上一页">上一页</asp:hyperlink>&nbsp;
            <asp:hyperlink id="HyperLink3" runat="server" ToolTip="下一页">下一页</asp:hyperlink>&nbsp;
            <asp:hyperlink id="HyperLink4" runat="server" ToolTip="尾页">尾页</asp:hyperlink></TD>
           <TD align="right">分页:<A id="first" title="首页" href="#" runat="server"><font face="webdings">9</font></A>
            <A id="prev10" title="上十页" href="#" runat="server"><font face="webdings">7</font></A>
            <b><A id="A1" href="#" runat="server">1</A></b> <b><A id="A2" href="#" runat="server">2</A></b>
            <b><A id="A3" href="#" runat="server">3</A></b> <b><A id="A4" href="#" runat="server">4</A></b>
            <b><A id="A5" href="#" runat="server">5</A></b> <b><A id="A6" href="#" runat="server">6</A></b>
            <b><A id="A7" href="#" runat="server">7</A></b> <b><A id="A8" href="#" runat="server">8</A></b>
            <b><A id="A9" href="#" runat="server">9</A></b> <b><A id="A10" href="#" runat="server">10</A></b>
            <A id="next10" title="下十页" href="#" runat="server"><font face="webdings">8</font></A>
            <A id="last" title="尾页" href="#" runat="server"><font face="webdings">:</font></A>
            跳转到第
            <asp:textbox id="TextBox1" runat="server" Width="30px" MaxLength="10"></asp:textbox>页
            <asp:imagebutton id="ImageButton1" runat="server" BorderWidth="0px" ImageUrl="images/go.gif" AlternateText="GO"></asp:imagebutton></B></B></TD>
          </TR>
         </TBODY>
        </TABLE>
       </TD>
      </TR>
     </TBODY>
    </TABLE>
   </FONT>
  </form>
 </body>
</HTML>
---------------------------------------------

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;
using X_System;

namespace XMLYB
{
 /// <summary>
 /// DataGrid2 的摘要说明。
 /// </summary>
 public class DataGrid2 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.HyperLink HyperLink1;
  protected System.Web.UI.WebControls.HyperLink HyperLink2;
  protected System.Web.UI.WebControls.HyperLink HyperLink3;
  protected System.Web.UI.WebControls.HyperLink HyperLink4;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
 
  int page=1,pagesize=20,pagecount=1;

  protected System.Web.UI.HtmlControls.HtmlAnchor first;
  protected System.Web.UI.HtmlControls.HtmlAnchor prev10;
  protected System.Web.UI.HtmlControls.HtmlAnchor next10;
  protected System.Web.UI.HtmlControls.HtmlAnchor A1;
  protected System.Web.UI.HtmlControls.HtmlAnchor A2;
  protected System.Web.UI.HtmlControls.HtmlAnchor A3;
  protected System.Web.UI.HtmlControls.HtmlAnchor A4;
  protected System.Web.UI.HtmlControls.HtmlAnchor A5;
  protected System.Web.UI.HtmlControls.HtmlAnchor A6;
  protected System.Web.UI.HtmlControls.HtmlAnchor A7;
  protected System.Web.UI.HtmlControls.HtmlAnchor A8;
  protected System.Web.UI.HtmlControls.HtmlAnchor A9;
  protected System.Web.UI.HtmlControls.HtmlAnchor A10;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.ImageButton ImageButton1;
  protected System.Web.UI.HtmlControls.HtmlAnchor last;

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!this.IsPostBack)
   {
    if(Request.QueryString["page"]!=null)
    {
     try
     {
      page = int.Parse(Request.QueryString["page"].ToString());
     }
     catch
     {
     }
    }

    PageBind();
    BindGrid();
    ButtonBind();
    PageItem();
   }
  }

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

  }
  #endregion

  private void BindGrid()
  {
   string sql = "select * from Orders";
   SqlDataAdapter da = new SqlDataAdapter(sql,SysPub.GetSqlStr2());
   DataSet ds = new DataSet();
   da.Fill(ds,(page-1)*pagesize,pagesize,"Orders");

   this.DataGrid1.DataSource = ds;
   this.DataGrid1.DataBind();

   ds.Clear();
   ds.Dispose();

   da.Dispose();
  }

  private void PageBind()
  {
   this.Label4.Text = page.ToString();

   string sql = "select count(1) from Orders";
   int all = (int)SysData.ExecuteScalar(SysPub.GetSqlStr2(),CommandType.Text,sql);

   this.Label2.Text = all.ToString();

   if(all>pagesize)
   {
    pagecount = all % pagesize;
    if(pagecount == 0)
     pagecount = all / pagesize;
    else
     pagecount = all / pagesize + 1;
   }
   
   if(page<0 || page>pagecount)
   {
    Response.Redirect(this.Request.CurrentExecutionFilePath);
    Response.End();
   }

   this.Label3.Text = pagecount.ToString();
  }

  private void ButtonBind()
  {
   this.HyperLink1.NavigateUrl = this.Request.CurrentExecutionFilePath + "?page=1";
   this.HyperLink2.NavigateUrl = this.Request.CurrentExecutionFilePath + "?page=" + (page-1);
   this.HyperLink3.NavigateUrl = this.Request.CurrentExecutionFilePath + "?page=" + (page+1);
   this.HyperLink4.NavigateUrl = this.Request.CurrentExecutionFilePath + "?page=" + pagecount;

   this.HyperLink1.Enabled = (page == 1 ? false : true);
   this.HyperLink2.Enabled = (page == 1 ? false : true);
   this.HyperLink3.Enabled = (page == pagecount ? false : true);
   this.HyperLink4.Enabled = (page == pagecount ? false : true);
  }

  private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if(e.Item.ItemIndex != -1)
   {
    Label lb1 = (Label)e.Item.Cells[0].FindControl("Label1");
    lb1.Text += (page - 1) * pagesize + e.Item.ItemIndex + 1;

    if(e.Item.Cells[1].Text == "10248")
    {
     e.Item.Cells[1].Text = "<font color=red>haha</font>";
     e.Item.Cells[1].ToolTip = "hehe";
     e.Item.Cells[1].Font.Bold = true;
    }

    if(e.Item.Cells[1].Text == "10249")    
     e.Item.Cells[1].Text = "<font color=blue>" + e.Item.Cells[1].Text + "</font>";

    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='Gainsboro'");

    if(e.Item.ItemType == ListItemType.Item)
     e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#EEEEEE'");

    if(e.Item.ItemType == ListItemType.AlternatingItem)
     e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='Gainsboro'");

    e.Item.Cells[1].Attributes.Add("onclick","location.href='http://www.zz.ha.cn'");
    e.Item.Cells[1].ToolTip = "单击";

    e.Item.Cells[2].Attributes.Add("ondblclick","location.href='http://www.valr.net'");
    e.Item.Cells[2].ToolTip = "双击";
   }
  }

  private void PageItem()
  {
   this.TextBox1.Text = page.ToString();

   int a1 = (page%10==0 ? (page-9) : ((page/10)*10+1));
   int a2 = (page%10==0 ? (page-8) : ((page/10)*10+2));
   int a3 = (page%10==0 ? (page-7) : ((page/10)*10+3));
   int a4 = (page%10==0 ? (page-6) : ((page/10)*10+4));
   int a5 = (page%10==0 ? (page-5) : ((page/10)*10+5));
   int a6 = (page%10==0 ? (page-4) : ((page/10)*10+6));
   int a7 = (page%10==0 ? (page-3) : ((page/10)*10+7));
   int a8 = (page%10==0 ? (page-2) : ((page/10)*10+8));
   int a9 = (page%10==0 ? (page-1) : ((page/10)*10+9));
   int a10 = (page%10==0 ? page : ((page/10)*10+10));

   A1.InnerHtml =(a1==page ? ("<font color=#ff0000>"+a1.ToString()+"</font>"): a1.ToString());
   A2.InnerHtml =(a2==page ? ("<font color=#ff0000>"+a2.ToString()+"</font>"): a2.ToString());
   A3.InnerHtml =(a3==page ? ("<font color=#ff0000>"+a3.ToString()+"</font>"): a3.ToString());
   A4.InnerHtml =(a4==page ? ("<font color=#ff0000>"+a4.ToString()+"</font>"): a4.ToString());
   A5.InnerHtml =(a5==page ? ("<font color=#ff0000>"+a5.ToString()+"</font>"): a5.ToString());
   A6.InnerHtml =(a6==page ? ("<font color=#ff0000>"+a6.ToString()+"</font>"): a6.ToString());
   A7.InnerHtml =(a7==page ? ("<font color=#ff0000>"+a7.ToString()+"</font>"): a7.ToString());
   A8.InnerHtml =(a8==page ? ("<font color=#ff0000>"+a8.ToString()+"</font>"): a8.ToString());
   A9.InnerHtml =(a9==page ? ("<font color=#ff0000>"+a9.ToString()+"</font>"): a9.ToString());
   A10.InnerHtml =(a10==page ? ("<font color=#ff0000>"+a10.ToString()+"</font>"): a10.ToString());

   A1.HRef = (a1==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a1.ToString()));
   A2.HRef = (a2==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a2.ToString()));
   A3.HRef = (a3==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a3.ToString()));
   A4.HRef = (a4==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a4.ToString()));
   A5.HRef = (a5==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a5.ToString()));
   A6.HRef = (a6==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a6.ToString()));
   A7.HRef = (a7==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a7.ToString()));
   A8.HRef = (a8==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a8.ToString()));
   A9.HRef = (a9==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a9.ToString()));
   A10.HRef = (a10==page ? null : (this.Request.CurrentExecutionFilePath + "?page=" + a10.ToString()));

   A1.Visible = (a1 <= pagecount ? true : false);
   A2.Visible = (a2 <= pagecount ? true : false);
   A3.Visible = (a3 <= pagecount ? true : false);
   A4.Visible = (a4 <= pagecount ? true : false);
   A5.Visible = (a5 <= pagecount ? true : false);
   A6.Visible = (a6 <= pagecount ? true : false);
   A7.Visible = (a7 <= pagecount ? true : false);
   A8.Visible = (a8 <= pagecount ? true : false);
   A9.Visible = (a9 <= pagecount ? true : false);
   A10.Visible = (a10 <= pagecount ? true : false);

   first.HRef = this.Request.CurrentExecutionFilePath + "?page=1";
   first.Visible = (page>1 ? true:false);

   prev10.HRef = this.Request.CurrentExecutionFilePath + "?page=" + (((page-1)/10)*10).ToString();
   prev10.Visible = (page>10 ? true:false);

   next10.HRef = this.Request.CurrentExecutionFilePath + "?page=" + (((((page-1)/10)+1)*10)+1).ToString();
   next10.Visible = ((((page-1)/10)+1)*10 <pagecount ? true:false);

   last.HRef = this.Request.CurrentExecutionFilePath + "?page=" + pagecount.ToString();
   last.Visible = (page<pagecount ? true:false);
  }

  private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   PageChange();
  }

  private void TextBox1_TextChanged(object sender, System.EventArgs e)
  {
   PageChange();
  }

  private void PageChange()
  {
   try
   {
    page = Convert.ToInt32(this.TextBox1.Text.Trim().ToString());
   }
   catch
   {
   }

   PageBind();
   BindGrid();
   ButtonBind();
   PageItem();
  }
 }
}

内容概要:本文探讨了在MATLAB/SimuLink环境中进行三相STATCOM(静态同步补偿器)无功补偿的技术方法及其仿真过程。首先介绍了STATCOM作为无功功率补偿装置的工作原理,即通过调节交流电压的幅值和相位来实现对无功功率的有效管理。接着详细描述了在MATLAB/SimuLink平台下构建三相STATCOM仿真模型的具体步骤,包括创建新模型、添加电源和负载、搭建主电路、加入控制模块以及完成整个电路的连接。然后阐述了如何通过对STATCOM输出电压和电流的精确调控达到无功补偿的目的,并展示了具体的仿真结果分析方法,如读取仿真数据、提取关键参数、绘制无功功率变化曲线等。最后指出,这种技术可以显著提升电力系统的稳定性与电能质量,展望了STATCOM在未来的发展潜力。 适合人群:电气工程专业学生、从事电力系统相关工作的技术人员、希望深入了解无功补偿技术的研究人员。 使用场景及目标:适用于想要掌握MATLAB/SimuLink软件操作技能的人群,特别是那些专注于电力电子领域的从业者;旨在帮助他们学会建立复杂的电力系统仿真模型,以便更好地理解STATCOM的工作机制,进而优化实际项目中的无功补偿方案。 其他说明:文中提供的实例代码可以帮助读者直观地了解如何从零开始构建一个完整的三相STATCOM仿真环境,并通过图形化的方式展示无功补偿的效果,便于进一步的学习与研究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值