一、web窗体建立

右键web应用程序,选择web窗体建立:获得.aspx页面,其中包含.aspx.cs页面和aspx.designer.cs页面。

.aspx页面继承于.aspx.cs页面,所以在.aspx页面可以访问父类的共享的数据。

MyFirstWebForm.aspx(前端)页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyFirstWebForm.aspx.cs" Inherits="WebApplicationModle._2024_10_18.VebFromAspx.MyFirstWebForm" %>
Page Language:使用的语言

AutoEventWireup:支持事件

CodeBehind:代码后置类

Inherits:继承的类型

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyFirstWebForm.aspx.cs" Inherits="WebApplicationModle._2024_10_18.VebFromAspx.MyFirstWebForm" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            
        </div>
    </form>
</body>
</html>

 MyFirstWebForm.aspx.cs(代码后置类)页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplicationModle._2024_10_18.VebFromAspx
{
    public partial class MyFirstWebForm : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
    }
}

MyFirstWebForm.aspx.designer.cs页面代码:

//------------------------------------------------------------------------------
// <自动生成>
//     此代码由工具生成。
//
//     对此文件的更改可能导致不正确的行为,如果
//     重新生成代码,则所做更改将丢失。
// </自动生成>
//------------------------------------------------------------------------------

namespace WebApplicationModle._2024_10_18.VebFromAspx
{


    public partial class MyFirstWebForm
    {

        /// <summary>
        /// form1 控件。
        /// </summary>
        /// <remarks>
        /// 自动生成的字段。
        /// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
        /// </remarks>
        protected global::System.Web.UI.HtmlControls.HtmlForm form1;
    }
}

二、案例

实现前台显示数据

过程:前端页面加载完后,通过Page_Load初始化,实现数据显示。注意前端可以通过<% %>访问后端,执行C#代码。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyFirstWebForm.aspx.cs" Inherits="WebApplicationModle._2024_10_18.VebFromAspx.MyFirstWebForm" %>


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>
                <%= this.SName %>
            </h1>
        </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplicationModle._2024_10_18.VebFromAspx
{
    public partial class MyFirstWebForm : System.Web.UI.Page
    {
        public string SName { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SName = "张三";
        }
    }
}

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐