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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - sooloo

asp.net遍历控件的实现 [转]Creating Custom Web Controls in C# Stats(演示了如何创建一个导航条) [转]创建动态数据输入用户界面(ASP.NET 中的动态控件入门) [转]ASP.NET 页面对象模型 [转]基于功能更丰富的基础类构建您自己的 ASP.NET 页面 [转]ASP.NET中促进代码重用的2种模式 [原创]利用CSS实现页面换肤 [转] more than one way to skin an app [转]模拟Asp.Net Forums实现可以换皮肤的控件 [转]在ASP.Net中两种利用CSS实现多界面的方法 DotText分析---新Blog注册 转:dotext数据库篇 Vs2003调试DotText时碰到的一个问题 DotText数据库分析(2) CnDotText数据库分析(1) 高程历年试题及答案 [转帖]追MM与设计模式 学习cnblogsGuestBook V2.0 --(2) 学习cnblogsGuestBook V2.0
[转] 动态加载Asp.net分页控件
sooloo · 2005-09-22 · via 博客园 - sooloo

  http://dev.csdn.net/develop/article/53/53566.shtm

动态加载Asp.net分页控件

郑佐2004-11-30

         asp.net中动态加载控件比较简单,这里我讲得是对用户控件的加载,比较典型的就是被加载的用户控件里面包含回发事件,在回传回来的时候需要保持新的数据。

         先来构建分页用户控件,由于前面几篇文章都在讲这些东西,所以就直接拿过来改了改,分页代码可以查看上面的文章1文章2,下面是用户控件的一部分代码。

public class PagingControl : System.Web.UI.UserControl

{

     private int pageCount;

     private int recordCount;

     ……

     private void Page_Load(object sender, System.EventArgs e)

     {

         if(!Page.IsPostBack)

         {

              DataGridDataBind();

         }

     }

     //绑定数据

     private void DataGridDataBind()

     {

         DataSet ds = GetCustomersData(PageIndex,PageSize,ref recordCount,ref pageCount);

         this.DataGrid1.VirtualItemCount = RecordCount;

         this.DataGrid1.DataSource = ds;

         this.DataGrid1.DataBind();

         SetPagingState();

     }

     //绑定新的页面

private void LBtnNavigation_Click(object sender, System.EventArgs e)

     {

         LinkButton btn = (LinkButton)sender;

         switch(btn.CommandName)

         {

              case "First":

                   PageIndex = 0;

                   break;

              case "Prev":

                   PageIndex = PageIndex - 1;

                   break;

              case "Next":

                   PageIndex = PageIndex + 1;

                   break;

              case "Last":

                   PageIndex = PageCount - 1;

                   break;

         }

         DataGridDataBind();             

     }

……

}

在上面我们注意到在页面Load事件中判断if(!Page.IsPostBack)来防止在回发加载的时候进行两次绑定,因为第一次完全没有必要,最终由LBtnNavigation_Click中的绑定决定。

假设PagingControl.ascx为上面的用户控件的文件名,而且同Page页面文件在同一目录,下面是AspnetCommonPaging.aspx文件的代码,为了动态加载,在页面上放了一个PlaceHolder控件来加载先前的用户控件。

前台文件如下:

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

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

<HTML>

  <HEAD>

                   <title>AspnetCommonPaging</title>                  

  </HEAD>

         <body>

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

<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>

                   </form>

         </body>

</HTML>

后台代码文件也比较简单:

namespace AspnetPaging

{

     public class AspnetCommonPaging : System.Web.UI.Page

     {

         protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;

         private void Page_Load(object sender, System.EventArgs e)

         {

              PlaceHolder1.Controls.Add(Page.LoadControl("~/PagingControl.ascx"));

         }

         #region Web 窗体设计器生成的代码

         override protected void OnInit(EventArgs e)

         {

              InitializeComponent();

              base.OnInit(e);

         }

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {   

              this.Load += new System.EventHandler(this.Page_Load);

         }

         #endregion

     }

}

这里的PlaceHolder1.Controls.Add(Page.LoadControl("~/PagingControl.ascx"));就是把PagingControl用户控件载到当前页面上来。如果不想用PlaceHolder,我们也可以使用其他容器控件,只要添加到Controls集合就行了。

注意这里如果按下面这样处理,那面在页面回发的时候就会不被加载,那么也就不会触发分页事件。

private void Page_Load(object sender, System.EventArgs e)

{

     if(!Page.IsPostBack)

         PlaceHolder1.Controls.Add(Page.LoadControl("~/PagingControl.ascx"));

}

现在让我们来看看主要事件的执行顺序:设置断点,得到下面的顺序。

第一次:页面OnInit事件-->页面Page_Load事件à控件OnInit事件à控件Page_Load事件。

翻页回发:页面OnInit事件-->页面Page_Load事件à控件OnInit事件à控件Page_Load事件à LBtnNavigation_Click翻页事件。

如果我们把页面加载代码放在页面OnInit事件:

override protected void OnInit(EventArgs e)

{

     InitializeComponent();

     PlaceHolder1.Controls.Add(Page.LoadControl("~/PagingControl.ascx"));

     base.OnInit(e);

}

那么事件的执行顺序会是什么样呢?

第一次:页面OnInit事件-->控件OnInit事件à页面Page_Load事件à控件Page_Load事件。

翻页回发:页面OnInit事件-->控件OnInit事件à页面Page_Load事件à控件Page_Load事件à LBtnNavigation_Click翻页事件。

我认为对于多控件的互操作,清楚事件的执行顺序是很重要的,另外也有助于优化程序提高性能。

posted on 2005-09-22 14:28  sooloo  阅读(862)  评论()    收藏  举报