对DataGrid的分页栏的理解

   昨天在Csdn回答一位朋友的问题时,恰巧碰到这样的问题,我有些疑惑,所以晚上回家就试了一下,后来才发现是自已错啦.在这里我想纠正自已的错误.

DataGrid的分页栏控件的组成及各自的功能

1.DataGrid的分页栏由三个控件组成.分别为:

1.System.Web.UI.WebControls.Label

2.System.Web.UI.LiteralControl

3.System.Web.UI.WebControls.DataGridLinkButton

其中每个控件有每个控件的作用.
##Label负责显示当前页,也就是选择页.
##LiteralControl负责显示一个空格,用来间隔它们之间的距离
##DataGridLinkButton显示连接,用来连接到其它页,如下一页.
这样就可以很轻松地回答昨于那位朋友的问题啦

原题:
[小弟想要做DataGrid,什么效果呢,要分页
1 2 3 4,当我选中第2页时,2的颜色要跟1 3 4不一样。
就像CSDN的分页一样一样一样的啊]

实现:

None.gif namespace  Text_WebApp
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// ChangPageStyle 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ChangPageStyle : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DataGrid1.DataSource
=CreateDataBase();
InBlock.gif                DataGrid1.DataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public DataTable CreateDataBase() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable Dt
=new DataTable();
InBlock.gif            Dt.Columns.Add(
"Id");
InBlock.gif            Dt.Columns.Add(
"Name");
InBlock.gif            Dt.Columns.Add(
"Zip");
InBlock.gif
InBlock.gif            DataRow Dr;
InBlock.gif            
for(int i=0;i<20;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Dr
=Dt.NewRow();
InBlock.gif                Dr[
"Id"]=i.ToString();
InBlock.gif                Dr[
"Name"]="姓名"+i.ToString();
InBlock.gif                Dr[
"Zip"]="地址"+i.ToString();
InBlock.gif                Dt.Rows.Add(Dr);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return Dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif            
this.DataGrid1.ItemCreated+=new DataGridItemEventHandler(DataGrid1_ItemCreated);
InBlock.gif            
this.DataGrid1.PageIndexChanged+=new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.Item.ItemType==ListItemType.Pager)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
foreach(Control Ct in e.Item.Cells[0].Controls)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif
                    switch(Ct.GetType().ToString())
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case "System.Web.UI.WebControls.Label"://当前页
InBlock.gif
                            Label Lb=(Label)Ct;
InBlock.gif                            Lb.ForeColor
=Color.Red;
InBlock.gif                            Lb.Font.Bold
=true;
InBlock.gif                            Lb.Text
="当前页["+Lb.Text+"]";
InBlock.gif                            
break;
InBlock.gif                        
case "System.Web.UI.LiteralControl"://页标识间间隔
InBlock.gif
                            LiteralControl Lt=(LiteralControl)Ct;
InBlock.gif                            Lt.Text
="&nbsp;&nbsp;<font color=blue>|</font>&nbsp;&nbsp";
InBlock.gif                            
break;
InBlock.gif                        
case "System.Web.UI.WebControls.DataGridLinkButton"://连接到其它页的LinkButton
InBlock.gif
                            LinkButton LB=(LinkButton)Ct;
InBlock.gif                            LB.Text
="第["+LB.Text+"]页";
InBlock.gif                            LB.ForeColor
=Color.Red;
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataGrid1.CurrentPageIndex
=e.NewPageIndex;
InBlock.gif            DataGrid1.DataSource
=CreateDataBase();
InBlock.gif            DataGrid1.DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

效果图:
Pages.gif


posted on 2004-07-19 10:45 笨笨 阅读(115) 评论(2)  编辑 收藏

评论

 re: 对DataGrid的分页栏的理解

我常用这样的方法:
        '突出显示当前页
        Dim Item As DataGridItem
        Dim ctrl As Control
        For Each Item In dg.Controls(0).Controls
            If Item.ItemType = ListItemType.Pager Then
                For Each ctrl In Item.Cells(0).Controls
                    If ctrl.GetType.ToString = "System.Web.UI.WebControls.Label" Then
                        If CType(ctrl, Label).Text = (dg.CurrentPageIndex + 1).ToString Then
                            CType(ctrl, Label).ForeColor = Color.Red
                            Exit For
                        End If
                    End If
                Next
            End If
        Next
#  re: 对DataGrid的分页栏的理解 我常用这样的方法:
        '突出显示当前页
        Dim Item As DataGridItem
        Dim ctrl As Control
        For Each Item In dg.Controls(0).Controls
            If Item.ItemType = ListItemType.Pager Then
                For Each ctrl In Item.Cells(0).Controls
                    If ctrl.GetType.ToString = "System.Web.UI.WebControls.Label" Then
                        If CType(ctrl, Label).Text = (dg.CurrentPageIndex + 1).ToString Then
                            CType(ctrl, Label).ForeColor = Color.Red
                            Exit For
                        End If
                    End If
                Next
            End If
        Next

转载于:https://www.cnblogs.com/miaomiaoga/archive/2004/11/10/62117.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值