Implements simple background code Templates using class inheritance relationships, and inherits background code templates.

Source: Internet
Author: User

Implements simple background code Templates using class inheritance relationships, and inherits background code templates.

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.

The experiment code is as follows:

The first is the template class abstraction, inherited from the Page class;

 1     public class Template : System.Web.UI.Page 2     { 3  4         protected override void OnLoad(EventArgs e) 5         { 6             base.OnLoad(e); 7  8             if (!IsPostBack) 9             {10                 DataRefresh();11             }12         }13 14         protected virtual void DataRefresh()15         {16         }17 18         protected virtual void grid_Init(object sender, EventArgs e)19         {20             var grid = sender as GridView;21             grid.AllowPaging = true;22             grid.PageSize = 10;23             grid.PagerSettings.Mode = PagerButtons.NumericFirstLast;24         }25 26         protected virtual void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)27         {28             var grid = sender as GridView;29             grid.PageIndex = e.NewPageIndex;30             DataRefresh();31         }32     }

Then there is a specific instance that inherits this template class;

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Child.aspx.cs" Inherits="WebTest.Child" %> 2  3 <!DOCTYPE html> 4  5 

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;

 1 public partial class Child : Template 2     { 3  4         protected override void DataRefresh() 5         { 6             grid.DataSource = new LogDao().GetLogsByYearMonth("2012", "10"); 7             grid.DataBind(); 8         } 9 10         protected override void grid_Init(object sender, EventArgs e)11         {12             base.grid_Init(sender, e);13 14             grid.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;15             grid.PageSize = 15;16         }17 18     }

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

 

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.