1

I need to pass an int from aspx.cs page to aspx page and show it there

relevant part of example.aspx.cs page:

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

<%@ 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.

umd3330
  • 39
  • 1
  • 8
  • can you not use `QueryString` here..? or better yet for example you could do something like this `

    <%=Id%>

    `
    – MethodMan Aug 31 '15 at 19:41
  • Possible duplicate of : http://stackoverflow.com/questions/7406961/how-to-call-a-variable-in-code-behind-to-aspx-page – JGV Aug 31 '15 at 19:46

2 Answers2

6

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

Habib
  • 219,104
  • 29
  • 407
  • 436
1

Injecting Code <%= //some code here %> into the HTML it is possible with ASP.NET. However I will recommend to use a literal control instead to always keep the separation between the controller and the UI. Better may be:

Html:

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

Code:

Literal1.Text = "My Value"
Dalorzo
  • 19,834
  • 7
  • 55
  • 102