asp.net WebForm程式刪除.designer.cs檔案之後的故事

來源:互聯網
上載者:User

標籤:des   blog   http   io   os   使用   ar   java   for   

1.介紹正常情況下添加一個WebForm程式結構如下(命名為:myWebForm.aspx)

檔案說明:.aspx檔案:書寫html代碼部分,以及javascript,css等代碼書寫及引用

                  .aspx.cs檔案:伺服器端,使用C#代碼處理用戶端發過來的請求,做出相應的響應

                  .aspx.designer.cs檔案:視圖表單建立檔案,繪製在伺服器端需要的控制項,即相當於初始化的部分

現在的需求是:將.aspx.designer.cs檔案刪除,在頁面類裡面添加一個在伺服器端啟動並執行控制項Button和TextBox,當點擊Button按鈕時為TextBox賦值“Hello”,同時進行頁面登入的校正.(當刪除此檔案後,在頁面通過工具箱添加伺服器端控制項是會報錯)

所以我們要進行一系列的修改,重寫一些方法,添加綁定事件,具體實施方法如下:2.步驟1.添加兩個WebForm表單,命名為:myWebForm.aspx  和  FormBase.aspx2.在myWebForm.aspx檔案內添加兩個控制項,代碼如下[html] view plaincopy 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myWebForm.aspx.cs" Inherits="myWebApplication.myWebForm" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.          <%--添加兩個控制項,並添加runat="server"屬性--%>  
  12.         <input id="myTxt" type="text" runat="server" />  
  13.         <input id="myBtn" type="button" value="button" runat="server" />  
  14.     </div>  
  15.     </form>  
  16. </body>  
  17. </html>  
3.刪除檔案,將兩個表單中的.aspx.designer.cs(將裡面定義的控制項變數語句可以先複製出來)4.編輯FormBase.aspx檔案,重寫方法,代碼如下:[csharp] view plaincopy 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace myWebApplication  
  9. {  
  10.     public partial class FormBase : System.Web.UI.Page  
  11.     {  
  12.         //所有繼承此類的頁面在開啟前都會先運行此類裡的Page_Load方法  
  13.          //更改方法的修飾符  
  14.         private void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             //do somethings  
  17.             //可以在此處進行頁面登入校正處理  
  18.         }  
  19.         //重寫OnInit  
  20.         override protected void OnInit(EventArgs e)  
  21.         {  
  22.             InitializeComponent();  
  23.             base.OnInit(e);  
  24.         }  
  25.         private void InitializeComponent()  
  26.         {  
  27.             this.Load += new System.EventHandler(this.Page_Load);  
  28.         }  
  29.     }  
  30. }  


 

 

5.編輯myWebForm.aspx,繼承FormBase,並重寫方法,為頁面類的控制項添加事件 [csharp] view plaincopy 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace myWebApplication  
  9. {  
  10.     public partial class myWebForm : FormBase  
  11.     {  
  12.         protected System.Web.UI.HtmlControls.HtmlInputText myTxt;  
  13.         protected System.Web.UI.HtmlControls.HtmlInputButton myBtn;  
  14.   
  15.         //重寫OnInit  
  16.         protected override void OnInit(EventArgs e)  
  17.         {  
  18.             InitializeComponent();  
  19.             base.OnInit(e);//調用父類的OnInit  
  20.         }  
  21.         //添加事件  
  22.         private void InitializeComponent()  
  23.         {  
  24.             //為頁面類的id為myBtn的Button添加事件  
  25.             this.myBtn.ServerClick += new EventHandler(myBtn_ServerClick);  
  26.             //this.Load += new System.EventHandler(this.Page_Load);  
  27.         }  
  28.         protected void Page_Load(object sender, EventArgs e)  
  29.         {  
  30.             if (!Page.IsPostBack)  
  31.             {  
  32.                 //頁面第一次載入時會運行裡面的方法  
  33.                 //通過點擊提交表單的時候不會運行裡面的方法  
  34.             }  
  35.         }  
  36.         private void myBtn_ServerClick(object sender, EventArgs e)  
  37.         {  
  38.             //當點擊button時會觸發此事件  
  39.             string s = "Hello";  
  40.             myTxt.Value = s;  
  41.         }   
  42.     }  
  43. }  

 

6.效果,運行此介面,點擊button文字框會顯"Hello字樣"

 

7.總結

1.刪除多餘的檔案

2.通過繼承的方式對每個頁面進行登入校正

3.等等

asp.net WebForm程式刪除.designer.cs檔案之後的故事

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.