首页 > 编程 > .NET > 正文

ASP.NET 2.0 new features

2024-07-10 12:55:27
字体:
来源:转载
供稿:网友
 

from quickstart:

simplified code behind model new in 2.0

asp.net 2.0 introduces an improved runtime for code-behind pages that simplifies the connections between the page and code. in this new code-behind model, the page is declared as a partial class, which enables both the page and code files to be compiled into a single class at runtime. the page code refers to the code-behind file in the codefile attribute of the <%@ page %> directive, specifying the class name in the inherits attribute. note that members of the code behind class must be either public or protected (they cannot be private).

c# codebehind code separation
 
the advantage of the simplified code-behind model over previous versions is that you do not need to maintain separate declarations of server control variables in the code-behind class. using partial classes (new in 2.0) allows the server control ids of the aspx page to be accessed directly in the code-behind file. this greatly simplifies the maintenance of code-behind pages.



这对开发来说确实是一个改进,codebehind中去掉server control的声明会使代码"清爽"很多,看起来舒服多了。请看示例:

这对开发来说确实是一个改进,codebehind中去掉server control的声明会使代码"清爽"很多,看起来舒服多了。请看示例: 页面中我们放置一个textbox:test.aspx
<%@ page language="c#" codefile="test.aspx.cs" inherits="test_aspx" %>

<html>
<head>
    <title>test asp.net 2.0</title>
</head>
<body>
    <form runat="server">
      <asp:textbox id="textbox1" runat="server"/>
      <asp:button id="button1" text="click me" onclick="button1_click" runat="server"/>
      <br />
      <br />
    </form>
</body>
</html>test.aspx.csusing system;

public partial class test_aspx: system.web.ui.page
{
    protected void button1_click(object sender, eventargs e)
    {
        response.write ( "hi,you have entered the word: " + textbox1.text);
    }

}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表