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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 优雅旋律

四角号码 对照表 ScriptManager Bug Application_Start 不执行 loading 关机效果 ZT Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景] ZT 数据库范式浅解 ZT 数据库设计系列:数据库设计5步骤 ZT Web Control 开发系列(一) 页面的生命周期 MarcHandler (Marc ISO2709) Resharper 4.0 终于RC了... GridView、DetailsView、FormView 、Repeater、DataList的区别 (ZT) 常用正则表达式 ZT linq(update) 更新用 自动赋值方法 利用对象序列化将购物车保存在Cookie中 (ZT) ajax tab 动态设置主题(Theme) 一些注意 持久化数据 关于vs2008的Eval方法 重大失误
datapager分页问题 (点击两次)
优雅旋律 · 2008-03-25 · via 博客园 - 优雅旋律

今天看了一下ListView和DataPager配合做数据分页的教程,感觉很爽很方便,用在自己的项目上面时却出现了问题,具体表现在点击上一页、下一页或者数字跳转页面时通常要点两下才能有反应,而且有时候乱跳页。 

我开始测试的代码是这样的:

public partial class ListViewTest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!Page.IsPostBack)
            BindData();
    }
    
    
protected void BindData()
    {
        DBDataContext db 
= new DBDataContext();
        var ds 
= db.Category;
        ListView1.DataSource 
= ds;
        ListView1.DataBind();
        db.Dispose();
    }
}

出现如开始提及的问题,找了半天原因也没有找到。后来在国外的一个论坛上找到了同病相怜的人,有专家给出了一个解决方案。把Page_Load里的数据绑定移到Page_PreRender中,也就是:

public partial class ListViewTest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
//if (!Page.IsPostBack)
        
//    BindData();
    }protected void Page_PreRender(object sender, EventArgs e)
    {
        BindData();
    }
    
    
protected void BindData()
    {
        DBDataContext db 
= new DBDataContext();
        var ds 
= db.Category;
        ListView1.DataSource 
= ds;
        ListView1.DataBind();
        db.Dispose();
    }
}

试了一下分页果然正常了。难道是Page_Load来的太迟?不得而知。另外,还有一种方法同样可行:

public partial class ListViewTest : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!Page.IsPostBack)
            BindData();
    }
protected void Page_PreRender(object sender, EventArgs e)
    {
        
//BindData();
    }protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, 
false);
        BindData();
    }
protected void BindData()
    {
        DBDataContext db 
= new DBDataContext();
        var ds 
= db.Category;
        ListView1.DataSource 
= ds;
        ListView1.DataBind();
        db.Dispose();
    }
}

zt