關於ASP.NET頁面與代碼分離

來源:互聯網
上載者:User

如有不明白的地方歡迎加QQ群14670545 探討

1.我們先建立一個頁面看看具體如何:

<%@ 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"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />    </div>    </form></body></html>

2.來看它的後台檔案

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.下面注意一下WebForm1.aspx分頁檔檔案的頭部Inherits對象,它的值是MyWebSiteTest.WebForm1,再回到後台檔案的代碼,大的命名空間MyWebSiteTest,然後是個類(背景代碼塊是一個類檔案)只不過這個類繼承的是System.Web.UI.Page,這是所有asp.net的頁面的基類。只要是繼承於System.Web.UI.Page的類所與所在後台命名空間組合的真題 於 頁面頭標籤屬性Inherits的值一致,這樣後台代碼和前台頁面就關聯起來了。

不信我們可以將.cs檔案的WebForm1改成gggg,然後去<%@ page>標籤的Inherits屬性的MyWebSiteTest.WebForm1改成MyWebSiteTest.gggg。測試效果是OK的。

4.基於上面的測試,下面我們開始分離

①.建立檔案夾CodeDividual,在此檔案夾中建立頁面DividualPage.aspx,然後將.cs檔案刪掉只保留.aspx檔案:

<%@ 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"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title></head><body>    <form id="form1" runat="server">    <div>        <asp:Button ID="btnTest" runat="server" Text="Button" />    </div>    </form></body></html>

②.建立檔案夾CodeBehind,在此檔案夾中建立一個類DividualPage.cs(當然,這個名字不重要的,自己可以測試下就知道),它的代碼如下

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 ");            //給button按鈕添加click事件            Button modifyBtn = Page.FindControl("btnTest") as Button;            modifyBtn.Click += new System.EventHandler(btnTest_Click);        }        //定義一個名字為btnTest_Click的事件        private void btnTest_Click(object sender, EventArgs e)        {            Response.Write("haha...");        }    }}

到此結束,自己測下吧

ps:推薦看看斯坦福大學的開放視頻教學,編程方法學 第五課 ,對於類於對象、執行個體的講解非常棒,類就是模板,就是一些東西的綜合,就是工廠,它比較抽象,要理解好

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.