ASP. NET page and code separation

Source: Internet
Author: User

If you have any questions, add the QQ group.14670545Discussion

1. Create a page first to see how:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyWebSiteTest.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

2. Check its background file.

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace MyWebSiteTest{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void Button1_Click(object sender, EventArgs e)        {        }    }}

3. next, pay attention to the inherits object in the header of the webform1.aspx page file. Its value is mywebsitetest. webform1, return to the code of the background file, the large namespace mywebsitetest, and then a class (the background code block is a class file), but this class inherits system. web. UI. page, which is the base class of all Asp.net pages. As long as it is inherited from system. web. UI. the real question of the combination of the page class and the background namespace is the same as the value of the inherits attribute of the page header label, so that the background code is associated with the foreground page.

If you do not believe this, you can change the. CS file's webform1 to GGGG, and then change mywebsitetest. webform1 from the inherits attribute of the <% @ page> tag to mywebsitetest. GGGG. The test result is OK.

4. Based on the test above, let's start separation.

①. Create the codedividual folder, create dividualpage. aspx in the folder, and delete the. CS file to keep the. aspx file only:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DividualPage.aspx.cs" Inherits="MyWebSiteTest.CodeBehind.DividualPage" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

②. Create a New codebehind folder, and create a class dividualpage. CS in this folder (of course, this name is not important and you can test it). Its code is as follows:

Using system; using system. collections. generic; using system. LINQ; using system. web; // using system. web. UI. webcontrols; namespace mywebsitetest. codebehind {public class dividualpage: system. web. UI. page {protected void page_load (Object sender, eventargs e) {response. write ("this is the behind Code create"); // Add the click event button modifybtn = page to the button. findcontrol ("btntest") as button; modifybtn. click + = new system. eventhandler (btntest_click);} // defines the private void btntest_click (Object sender, eventargs e) {response. write ("Haha... ");}}}

Now, let's test it.

PS: it is recommended to take a look at open Video Teaching and programming methodology at Stanford University.Lesson 5The explanation of objects and instances is very good. Classes are templates and some things are integrated, that is, factories. They are abstract and need to be understood well.

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.