asp.net WebForm程序删除.designer.cs文件之后的故事

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.aspx

2.在myWebForm.aspx文件内添加两个控件,代码如下

[html]  view plain copy
 
  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 plain copy
 
  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 plain copy
 
  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.等等

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值