Dynamically load the Asp.net paging Control

Source: Internet
Author: User
In Asp.net, it is relatively simple to dynamically load controls. Here I am talking about the loading of user controls. A typical example is that the loaded user control contains a send-back event, new data must be retained when it is returned.

First, we will build a paging user control. As we have talked about these things in the previous articles, we have taken them and changed them. For the paging code, we can view article 1, article 2, the following is a part of the user control code.

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 ();

}

}

// Bind data

Private void DataGridDataBind ()

{

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

This. DataGrid1.VirtualItemCount = RecordCount;

This. DataGrid1.DataSource = ds;

This. DataGrid1.DataBind ();

SetPagingState ();

}

// Bind a new page

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 ();

}

......

}

In the above section, we noticed that the if (! Page. IsPostBack) to prevent bind twice during the re-sending and loading, because it is unnecessary for the first time and is determined by the binding in LBtnNavigation_Click.

Suppose PagingControl. ascx is the name of the above user control file and is in the same directory as the Page file. below is AspnetCommonPaging. to dynamically load the code of the aspx file, a PlaceHolder control is placed on the page to load the previous user control.

The foreground file is as follows:

<% @ 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>

The background code file is also relatively simple:

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 "));

}

 

# Code generated by region web Form Designer

Override protected void oninit (eventargs E)

{

Initializecomponent ();

Base. oninit (E );

}

/// <Summary>

/// The designer supports the required methods-do not use the code editor to modify

/// Content of this method.

/// </Summary>

Private void initializecomponent ()

{

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

}

# Endregion

}

}

 

PlaceHolder1.Controls. Add (Page. LoadControl ("~ /PagingControl. ascx ") is to load the PagingControl user control to the current page. If you do not want to use PlaceHolder, you can also use other container Controls as long as they are added to the Controls set.

Note that if you follow the steps below, the page will not be loaded when the page is sent back, so the paging event will not be triggered.

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

{

If (! Page. IsPostBack)

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

}

Now let's take a look at the execution sequence of the main events: Set the breakpoint to get the following sequence.

First time: Page oninit event --> page page_load event à control oninit event à control page_load event.

Page_load event & gt; Control oninit event & gt; Control page_load event & gt; lbtnnavigation_click page flip event.

If we place the page loading code in the oninit event of the page:

Override protected void oninit (eventargs E)

{

Initializecomponent ();

Placeholder1.controls. Add (page. loadcontrol ("~ /Pagingcontrol. ascx "));

Base. oninit (E );

}

What is the event execution sequence?

First time: Page oninit event --> Control oninit event à page page_load event à control page_load event.

Page flip back: Page oninit event --> Control oninit event à page page_load event à control page_load event à lbtnnavigation_click page flip event.

I think for multi-control interoperability, it is very important to know the sequence of event execution, and it also helps optimize programs to improve performance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.