不使用默认后置代码文件

来源:互联网 发布:贾跃亭事件始末 知乎 编辑:程序博客网 时间:2024/05/22 04:08

我们在用VS IDE创建程序时,在生成了起始页后,直接双击该起始页,就可对其进行后置代码文件的编缉。如果我们要改用其它的代码文件来做为该起始页的后置代码文件,要如何做呢?

以下是个简单示例:

起始页Default.aspx代码

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="default.aspx.cs" Inherits="_default" %>

<!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>Web Forms代码绑定实例</title>
</head>
<body>
    <h3>Web Forms代码绑定实例</h3>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnClick" runat="server" Text="看看效果" OnClick="btnClick_Click" />
        <hr />
        <asp:Label id="lblState" runat="server" />
     </div>
    </form>
</body>
</html>

 

codebehindcode.aspx.cs代码

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public class CodeBehindExample : Page
{
 public Label lblState;
 public Button btnClick;
 public static int n=0;

 public void btnClick_Click(Object Source, EventArgs e)
 {
  n=n+1;
  lblState.Text="您点击了"+n+"次按钮!";
 }
}

 

下面是关键:

将起始页Default.aspx代码开头处改为

<%@ Page Language="C#" AutoEventWireup="true"  Src="codebehindcode.aspx.cs" Inherits="CodeBehindExample" %>
说明:Src后接的是要使用的后置代码文件,Inherits的值为接的要使用的后置代码文件中定义的类,此处是CodeBehindExample

原创粉丝点击