Implement simple background Code Template Using the inheritance relationship of classes (override the parent class method)

Source: Internet
Author: User

The advantage of Asp.net is to quickly build applications. For some of the most basic data addition, deletion, modification, and paging event or style settings, you can write a virtual method in the parent class for the subclass to call, if the subclass needs to derive a change or simply not use a method of the parent class on the basis of the template, you only need to override the method of the parent class.

experiment Code is as follows :
first, the template class is abstracted and inherited from the page class; copy Code the code is as follows: public class template: system. web. UI. page
{< br> protected override void onload (eventargs e)
{< br> base. onload (E);
If (! Ispostback)
{< br> datarefresh ();
}< BR >}< br> protected virtual void datarefresh ()
{< BR >}< br> protected virtual void grid_init (Object sender, eventargs e)
{< br> var grid = sender as gridview;
grid. allowpaging = true;
grid. pagesize = 10;
grid. pagersettings. mode = pagerbuttons. numericfirstlast;
}< br> protected virtual void grid_pageindexchanging (Object sender, gridviewpageeventargs e)
{< br> var grid = sender as gridview;
grid. pageindex = E. newpageindex;
datarefresh ();
}< BR >}

then an instance that inherits the template class ; copy Code the code is as follows: <% @ page Language =" C # "autoeventwireup =" true "codebehind =" child. aspx. CS "inherits =" webtest. child "%>













Finally, it is the key background code implementation. Here we assume that you need to modify the number of displays per page and the paging style of the grid;Copy codeThe Code is as follows: public partial class child: Template
{
Protected override void datarefresh ()
{
Grid. datasource = new logdao (). getlogsbyyearmonth ("2012", "10 ");
Grid. databind ();
}
Protected override void grid_init (Object sender, eventargs E)
{
Base. grid_init (sender, e );
Grid. pagersettings. mode = pagerbuttons. nextpreviusfirstlast;
Grid. pagesize = 15;
}
}

The above is only a way of thinking. If there is any mistake, I hope all netizens will criticize and correct it.

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.