简体   繁体   English

将int从aspx.cs传递到aspx页面

[英]Pass int from aspx.cs to aspx page

I need to pass an int from aspx.cs page to aspx page and show it there 我需要将一个int从aspx.cs页传递到aspx页并在此处显示

relevant part of example.aspx.cs page: example.aspx.cs页面的相关部分:

public partial class example : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected int Id(){
            var Id = 318;
            return Id;
            }
    }

Relevant part of aspx page aspx页面的相关部分

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="example.aspx.cs" Inherits="blahblah.example" %>
.
.
.
<body>
    <h1>example.Id()</h1>
.
.
.

How can I edit this? 我该如何编辑? It should be straight foward, but I can't seem to figure it out. 它应该是直截了当的,但是我似乎无法弄清楚。

Simply do: 只需做:

<h1><%=Id()%></h1>

This will display the return value of the method. 这将显示该方法的返回值。

You may see: Introduction to ASP.NET inline expressions in the .NET Framework 您可能会看到: .NET Framework中的ASP.NET内联表达式简介

Injecting Code <%= //some code here %> into the HTML it is possible with ASP.NET. 使用ASP.NET可以将代码<%= //some code here %><%= //some code here %>注入HTML。 However I will recommend to use a literal control instead to always keep the separation between the controller and the UI. 但是,我建议改用文字控件,以始终保持控制器和UI之间的分隔。 Better may be: 更好的可能是:

Html: HTML:

<asp:Literal ID="Literal1" runat="server"></asp:Literal>

Code: 码:

Literal1.Text = "My Value"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM