Implementing a simple background code template with a class's inheritance relationship (overriding the parent class method) Practical Tips

Source: Internet
Author: User
Tags inheritance
The advantage of ASP.net is the rapid construction of applications, while for some of the most basic data additions and deletions and paging event or style settings can be written in the parent class virtual method to call the subclass, if subclasses need to derive changes on the basis of a template or simply do not want the parent-class method, then only the method of the parent class needs to be overridden.

The experimental code is as follows
The first is the abstraction of the template class, which inherits from the Page class;
Copy Code code as follows:

public class Template:System.Web.UI.Page
{
protected override void OnLoad (EventArgs e)
{
Base. OnLoad (e);
if (! IsPostBack)
{
Datarefresh ();
}
}
protected virtual void Datarefresh ()
{
}
protected virtual void Grid_init (object sender, EventArgs e)
{
var Grid = sender as GridView;
Grid. AllowPaging = true;
Grid. PageSize = 10;
Grid. Pagersettings.mode = Pagerbuttons.numericfirstlast;
}
protected virtual void Grid_pageindexchanging (object sender, Gridviewpageeventargs e)
{
var Grid = sender as GridView;
Grid. PageIndex = E.newpageindex;
Datarefresh ();
}
}

then a concrete instance that inherits this template class
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Child.aspx.cs" inherits= "Webtest.child"%>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "Grid" runat= "Server" oninit= "Grid_init" onpageindexchanging= "grid_pageindexchanging" ></ Asp:gridview>
</div>
</form>
</body>

Finally, the key backend code implementation, where you need to modify the grid's display number and page style;
Copy Code code 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.nextpreviousfirstlast;
Grid. PageSize = 15;
}
}

The above is only to provide a way of thinking, if there is a mistake to look at the netizens criticized.

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.