【问题标题】:Pass int from aspx.cs to aspx page将 int 从 aspx.cs 传递到 aspx 页面
【发布时间】:2015-08-31 19:39:39
【问题描述】:

我需要将一个 int 从 aspx.cs 页面传递到 aspx 页面并在那里显示

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;
            }
    }

aspx页面的相关部分

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

我该如何编辑这个?它应该是直截了当的,但我似乎无法弄清楚。

【问题讨论】:

标签: c# html asp.net .net


【解决方案1】:

简单地做:

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

这将显示方法的返回值。

你可能会看到:Introduction to ASP.NET inline expressions in the .NET Framework

【讨论】:

  • 成功了!谢谢@Habib,我之前在没有 % 的情况下试过这个。
  • @umd3330 请将此标记为解决您问题的解决方案
  • 谢谢@sab669。就是这样做的。社区新手:)
【解决方案2】:

将代码 &lt;%= //some code here %&gt; 注入 HTML 中,使用 ASP.NET 是可能的。但是,我建议使用文字控件来始终保持控制器和 UI 之间的分离。更好的可能是:

HTML:

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

代码:

Literal1.Text = "My Value"

【讨论】:

  • 感谢 Dalorzo,这是一个很好的技术。
猜你喜欢
  • 2012-06-25
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多