Implement simple background code template with class inheritance relation

Source: Internet
Author: User

The advantage of ASP is to quickly build applications, and for some of the most basic data additions and deletions, as well as paging events or style settings can be used to write a virtual method in the parent class for the subclass call, if the subclass needs to be based on the template to derive changes or simply not the parent class method, you only need to rewrite the parent class method.

The experiment code is as follows:

The first is the abstraction of the template class, inheriting from the page class;

1      Public classTemplate:System.Web.UI.Page2     {3 4         protected Override voidOnLoad (EventArgs e)5         {6             Base. OnLoad (e);7 8             if(!IsPostBack)9             {Ten Datarefresh (); One             } A         } -  -         protected Virtual voidDatarefresh () the         { -         } -  -         protected Virtual voidGrid_init (Objectsender, EventArgs e) +         { -             varGrid = Sender asGridView; +Grid. AllowPaging =true; AGrid. PageSize =Ten; atGrid. Pagersettings.mode =Pagerbuttons.numericfirstlast; -         } -  -         protected Virtual voidGrid_pageindexchanging (Objectsender, Gridviewpageeventargs e) -         { -             varGrid = Sender asGridView; inGrid. PageIndex =E.newpageindex; - Datarefresh (); to         } +}

Then is a concrete instance of inheriting this template class;

1 <%@ Page Language="C #"AutoEventWireup="true"codebehind="Child.aspx.cs"Inherits="Webtest.child" %>2 3 <!DOCTYPE HTML>4 5 <HTMLxmlns= "http://www.w3.org/1999/xhtml">6 <Headrunat= "Server">7 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>8     <title></title>9 </Head>Ten <Body> One     <formID= "Form1"runat= "Server"> A     <Div> -         <Asp:gridviewID= "Grid"runat= "Server"OnInit= "Grid_init"onpageindexchanging= "Grid_pageindexchanging"></Asp:gridview> -     </Div> the     </form> - </Body> - </HTML>

Finally, the key background code implementation, here is the assumption that you need to modify the grid of each page display number and pagination style;

1  Public Partial classchild:template2     {3 4         protected Override voidDatarefresh ()5         {6Grid. DataSource =NewLogdao (). Getlogsbyyearmonth (" -","Ten");7 grid. DataBind ();8         }9 Ten         protected Override voidGrid_init (Objectsender, EventArgs e) One         { A             Base. Grid_init (sender, E); -  -Grid. Pagersettings.mode =Pagerbuttons.nextpreviousfirstlast; theGrid. PageSize = the; -         } -  -}

The above is only to provide a way of thinking, if the wrong to hope that the netizens criticize.

Implement simple background code template with class inheritance relation

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.