DataGrid中页导航栏的自定义样式

我们可以利用DataGridItemCreat()方法来实现DataGrid中页脚(Foot)和页导航栏的自定义样式。ItemCreat,顾名思义,就是在数据项创建时发生的事件,对于DataGrid来讲,表头(Head)、数据项(DataItem)、页脚(Foot)、页导航(Pager)都是一个Item。下面的例子将实现页脚与页导航的自定义样式。我们希望在页脚现实DataGrid中的总页数,对于页导航中的LinkButton,我们希望能够以[<Page Index>]的格式来显示,对于当前页的页导航索引,我们希望显示成Page <PageIndex>的格式。

前台文件:

<%@ Page language="c#" Codebehind="Chp2_DGPagination.aspx.cs" AutoEventWireup="false" Inherits="DemoProject.Chp2_DGPagination" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<HTML>

     <HEAD>

         <title>Chp2_DGPagination</title>

         <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">

         <meta name="CODE_LANGUAGE" Content="C#">

         <meta name="vs_defaultClientScript" content="JavaScript">

         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

         <link href="myStyle.css" type="text/css" rel="stylesheet">

     </HEAD>

     <body MS_POSITIONING="GridLayout">

         <form id="Form1" method="post" runat="server">

<asp:DataGrid id="Products" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"

     BackColor="White" CellPadding="4" AutoGenerateColumns="False" Font-Size="X-Small" Font-Names="Verdana"

     AllowPaging="True" Width="600px" ShowFooter="True" GridLines="Horizontal">

     <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>

     <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>

     <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>

     <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>

     <Columns>

         <asp:BoundColumn DataField="ProductName" HeaderText="ProductName">

              <HeaderStyle Width="40%"></HeaderStyle>

         </asp:BoundColumn>

         <asp:BoundColumn DataField="QuantityPerUnit" HeaderText="QuantityPerUnit">

              <HeaderStyle Width="40%"></HeaderStyle>

         </asp:BoundColumn>

         <asp:BoundColumn DataField="UnitsInStock" HeaderText="UnitsInStock">

              <HeaderStyle Width="20%"></HeaderStyle>

         </asp:BoundColumn>

     </Columns>

     <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>

</asp:DataGrid>

         </form>

     </body>

</HTML>

 

后台文件:

 

None.gif using  System;
None.gif
None.gif
using  System.Collections;
None.gif
None.gif
using  System.ComponentModel;
None.gif
None.gif
using  System.Data;
None.gif
None.gif
using  System.Drawing;
None.gif
None.gif
using  System.Web;
None.gif
None.gif
using  System.Web.SessionState;
None.gif
None.gif
using  System.Web.UI;
None.gif
None.gif
using  System.Web.UI.WebControls;
None.gif
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
using  System.Configuration;
None.gif
None.gif
using  System.Data.SqlClient;
None.gif
None.gif 
None.gif
None.gif
namespace  DemoProject
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
using System;
InBlock.gif
InBlock.gif
using System.Collections;
InBlock.gif
InBlock.gif
using System.ComponentModel;
InBlock.gif
InBlock.gif
using System.Data;
InBlock.gif
InBlock.gif
using System.Drawing;
InBlock.gif
InBlock.gif
using System.Web;
InBlock.gif
InBlock.gif
using System.Web.SessionState;
InBlock.gif
InBlock.gif
using System.Web.UI;
InBlock.gif
InBlock.gif
using System.Web.UI.WebControls;
InBlock.gif
InBlock.gif
using System.Web.UI.HtmlControls;
InBlock.gif
InBlock.gif
using System.Configuration;
InBlock.gif
InBlock.gif
using System.Data.SqlClient;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif
namespace DemoProject
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif     
public class Chp2_DGPagination : System.Web.UI.Page
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif
InBlock.gif         
protected System.Web.UI.WebControls.DataGrid Products;
InBlock.gif
InBlock.gif     
InBlock.gif
InBlock.gif         
private void Page_Load(object sender, System.EventArgs e)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              
if(!Page.IsPostBack)
InBlock.gif
InBlock.gif                   SetBind();
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif         
private void SetBind()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              SqlConnection MyConn 
= new SqlConnection(ConfigurationSettings.AppSettings["conn"]);
InBlock.gif
InBlock.gif              SqlDataAdapter MyAdpt 
= new SqlDataAdapter("SELECT ProductID,ProductName,QuantityPerUnit,UnitsInStock FROM Products",MyConn);
InBlock.gif
InBlock.gif              DataSet MyDs 
= new DataSet();
InBlock.gif
InBlock.gif              MyAdpt.Fill(MyDs,
"Products");
InBlock.gif
InBlock.gif              DataTable MyTab 
= MyDs.Tables["Products"];
InBlock.gif
InBlock.gif              Products.DataSource 
= MyTab.DefaultView;
InBlock.gif
InBlock.gif              Products.DataBind();
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif         
Web#region Web 
InBlock.gif
InBlock.gif         
override protected void OnInit(EventArgs e)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              InitializeComponent();
InBlock.gif
InBlock.gif              
base.OnInit(e);
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
InBlock.gif
InBlock.gif         
private void InitializeComponent()
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{    
InBlock.gif
InBlock.gif              
this.Products.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.Products_ItemCreated);
InBlock.gif
InBlock.gif              
this.Products.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Products_PageIndexChanged);
InBlock.gif
InBlock.gif              
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
ExpandedSubBlockEnd.gif         
#endregion

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif         
private void Products_ItemCreated(object sender, DataGridItemEventArgs e)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              ListItemType elemType 
= e.Item.ItemType;
InBlock.gif
InBlock.gif              
if(elemType == ListItemType.Pager)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   TableCell pager 
= (TableCell)e.Item.Controls[0];
InBlock.gif
InBlock.gif                   
for(int i=0;i<pager.Controls.Count;i+=2)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                   
dot.gif{
InBlock.gif
InBlock.gif                       
object o = pager.Controls[i];
InBlock.gif
InBlock.gif                       
if(o is LinkButton)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif
InBlock.gif                            LinkButton h 
= (LinkButton) o;
InBlock.gif
InBlock.gif                            h.Text 
= "["+h.Text+"]";
InBlock.gif
ExpandedSubBlockEnd.gif                       }

InBlock.gif
InBlock.gif                       
else
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                       
dot.gif{
InBlock.gif
InBlock.gif                            Label l 
= (Label) o;
InBlock.gif
InBlock.gif                            l.Text 
= "Page " +l.Text;
InBlock.gif
ExpandedSubBlockEnd.gif                       }

InBlock.gif
ExpandedSubBlockEnd.gif                   }

InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
InBlock.gif              
if(elemType == ListItemType.Footer)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif              
dot.gif{
InBlock.gif
InBlock.gif                   TableCellCollection tcc 
= e.Item.Cells;
InBlock.gif
InBlock.gif                   
int nTotalCols = tcc.Count;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif                   
for(int i =0; i<nTotalCols-1;i++)
InBlock.gif
InBlock.gif                       e.Item.Cells.RemoveAt(i);
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif                   TableCell c 
= e.Item.Cells[0];
InBlock.gif
InBlock.gif                   c.ColumnSpan 
= nTotalCols;
InBlock.gif
InBlock.gif                   c.Text 
= Products.PageCount.ToString() + "pages found";
InBlock.gif
ExpandedSubBlockEnd.gif              }

InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif         
private void Products_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif
InBlock.gif              Products.CurrentPageIndex 
= e.NewPageIndex;
InBlock.gif
InBlock.gif              SetBind();
InBlock.gif
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockEnd.gif     }

InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedBlockEnd.gif}

None.gif



在后台代码中,有如下几点需要注意:

1,SetBind()方法用于实现DataGrid与数据源的绑定,因为每次在指定DataGrid的CurrentPageIndex属性后,都要重新绑定数据源才能实现分页;

2, InitializeComponent()方法中,添加如下两句代码,声明两个事件:

     this.Products.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.Products_ItemCreated);

     this.Products.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Products_PageIndexChanged);

3,ItemCreated()方法中的DataGridEventArgs参数e返回DataGrid创建的Item的类型,即ListItemType。Item就相当于一个TableRow,我们在属性生成器中生成的样式都是针对于<TR>的,所以如果对其中的内容自定义,只有通过ItemCreat方法。TableCell pager = (TableCell)e.Item.Controls[0]; 这句代码用pager来代替这个TableRow中的<TD>,而其中的内容(pager.Controls[])正是也导航按钮(LinkButton或Label),通过pager.Controls.Count返回控件数目,也就是页数,然后通过for循环结构,逐个判断pager中的控件类型,进行相应的转换。实现页导航的自定义;

4,用ItemCreated()方法同时实现了对Foot的自定义样式。Foot默认为与DataItem相同的带有n个单元格的TableRow,我们希望这些单元格合并后显示总页数。首先,通过TableCellCollection对象tcc的Removeat方法,以此移掉前n-1个单元格,留下最后一个,设置最后一个的ColumnSpan属性为n(相当于HTML标记中TD的COLSPAN属性),通过设置它的对齐、字体等样式及内容,既可以实现对Foot的自定义了。




或者:
None.gif private   void  dgShowC_ItemCreated( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(e.Item.ItemType == ListItemType.Pager)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
foreach (Control c in e.Item.Cells[0].Controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (c is Label)  //当前页数
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{
InBlock.gif                        Label lblpage
=(Label)c;
InBlock.gif                        
//      lblpage.ForeColor= System.Drawing.ColorTranslator.FromHtml("#e78a29"); //#e78a29 ,#FF0000     
InBlock.gif                        
//      lblpage.Font.Bold=true;
InBlock.gif
                        lblpage.Text="[<font color=#e78a29><b>"+lblpage.Text+"</b></font>]";     
InBlock.gif                        
//((Label)c).ForeColor = System.Drawing.Color.Green;      
InBlock.gif                        
//      break;
ExpandedSubBlockEnd.gif
                    }

InBlock.gif                    
if(c is LinkButton) //链接的其他页数
ExpandedSubBlockStart.gifContractedSubBlock.gif
                    dot.gif{      
InBlock.gif                        LinkButton linkButton 
= (LinkButton)c;       
InBlock.gif                        linkButton.Text 
= "[" + linkButton.Text+"]"
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }
    
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }






原贴:http://www.mscenter.edu.cn/blog/drummer/archive/2005/10/18/6249.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值