惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
Jina AI
Jina AI
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
I
InfoQ
L
LangChain Blog
The Cloudflare Blog
IT之家
IT之家
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
A
About on SuperTechFans
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
博客园 - 聂微东
美团技术团队
博客园_首页

博客园 - Airship

lrc 文件格式 不打算在cnblogs灌水文了,转移阵地到 http://airship.blogdriver.com 爱情保鲜——永和豆浆——MSDN翻译——1点18分 丁香花——有小成 I'm not the only one starin' at the sun 芦沟桥——抗日纪念馆 1个小时1000字 左手感觉怪怪的 停止翻译工作喽 头儿说要改版stuthu界面模版 B7和弦——丁香花——修炼中 GNU is a hack Done with The First MSDN essay 翻译的头晕脑胀,喘口气先 哈 做了个职业倾向测试 今天跟我们动振所所长聊了会儿 MSDN翻译工作启动 车公庄-源泽兴-天津一中 航天离我而远去 I know you will read this
DataGrid的翻页和动态显示
Airship · 2005-10-06 · via 博客园 - Airship

不能总是写水文了。今天编datagrid,顺便贴点常用代码。下面是一段通用的设置分页的代码。myd是我们的datagrid实例。

分页代码:

        private void Grid_Change(object sender,System.Web.UI.WebControls.DataGridPageChangedEventArgs e) 
        {
            myd.CurrentPageIndex 
= e.NewPageIndex;
            Get_Data(); 
        }

动态显示代码:

private void OnItemDataBound(object sender,    System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            
if (e.Item.ItemType!=ListItemType.Header && e.Item.ItemType!=ListItemType.Footer) 
            {
                
object di = e.Item.DataItem;
                
int isRead = Convert.ToInt16(DataBinder.Eval(di, "Is_Read"));
                
if (isRead == 0 )
                {
                    e.Item.Cells[
5].Text = "尚未回复";
                    
//e.Item.ForeColor = System.Drawing.Color.Red;  
                } 
                
else if ( isRead ==1)
                {
                    e.Item.Cells[
5].Text = "已经回复";
                }
            }

        }

然后要这样设置

private void InitializeComponent()
        {
            
this.myd.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Grid_Change);
            
this.myd.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.OnItemDataBound);
            
this.Load += new System.EventHandler(this.Page_Load);

        }

总结一下:这段代码是用在User Control里的。在Web Control里,我们用CreateChildControl的方法Create一个DataGrid,而这种利用DataGrid自带的分页机制是不能用在Web Control里的,引用Web Control时必然需要一个aspx页,这个aspx页的Page_Load机制,及Post_Back等机制,导致触发事件后,无法保存上一个状态。在Web Control里利用自带分页功能的方法还有待研究。