不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
ASP.NET 项目中 aspx 页面内联代码和代码分离演示示例
编辑:dnawo 日期:2026-06-22

在传统的 ASP.NET 项目中,aspx 页面输出内容有两种组织代码的方式:代码不分离(内联代码)和代码分离(CodeBehind),下面用简单的两个完整示例演示:
1、代码不分离(内联代码)
这种方式只有一个 .aspx 文件:
复制内容到剪贴板
程序代码
程序代码<%@ Page Language="C#" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello World - 内联代码</title>
</head>
<body>
<%
// 直接在页面中输出字符串
Response.Write("hello world.");
%>
<!-- 或者使用简写形式:<%="hello world."%> -->
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Hello World - 内联代码</title>
</head>
<body>
<%
// 直接在页面中输出字符串
Response.Write("hello world.");
%>
<!-- 或者使用简写形式:<%="hello world."%> -->
</body>
</html>
2、代码分离(CodeBehind)
这种方式有一个 .aspx 文件和一个 .aspx.cs 文件,通过 Inherits 和 CodeBehind 指令关联:
复制内容到剪贴板
程序代码
程序代码<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="HelloWorld_CodeBehind.aspx.cs"
Inherits="HelloWorld_CodeBehind" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello World - 代码分离</title>
</head>
<body>
<form id="form1" runat="server">
<!-- 用 Label 控件展示后台赋的值 -->
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
CodeBehind="HelloWorld_CodeBehind.aspx.cs"
Inherits="HelloWorld_CodeBehind" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello World - 代码分离</title>
</head>
<body>
<form id="form1" runat="server">
<!-- 用 Label 控件展示后台赋的值 -->
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
复制内容到剪贴板
程序代码
程序代码using System;
public partial class HelloWorld_CodeBehind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在页面加载时为 Label 赋值
lblMessage.Text = "hello world.";
}
}
public partial class HelloWorld_CodeBehind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在页面加载时为 Label 赋值
lblMessage.Text = "hello world.";
}
}
除此之外,还有一种称为服务器端脚本块的写法,像是上面两种的合体,也是只有一个 .aspx 文件:
复制内容到剪贴板
程序代码
程序代码<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello World - 服务器端脚本块</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</form>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "hello world.";
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Hello World - 服务器端脚本块</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</form>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
lblMessage.Text = "hello world.";
}
</script>
</body>
</html>
上一篇: 2026 吴越杯决赛
下一篇: ASP.NET 项目 Web Site Project 与 Web Application Project 的区别详解
文章来自: 本站原创
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 27
发表评论
请登录后再发表评论!



