2

First I have index.aspx and below that there is a c# class called index.aspx.cs

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

    public partial class Index : Page
    {
        public string test = "hello";
        protected void Page_Load(object sender, EventArgs e)
        {
            test = "Hello lesley";
        }
    }

But how can I show the value of the 'test' string in the html page?

1
  • drop a label on your aspx form, then use Label1.Text = test Commented Dec 3, 2015 at 12:32

3 Answers 3

4

In your markup use asp:Label as

<asp:Label runat="server" ID="MyLabel"></asp:Label>

And in your backend code use Text property accordingly

public string test = "hello";

if(!IsPostBack)
{
   MyLabel.Text = test;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@LesleyPeters Glad I could help.. Good luck with your project
2

Try this

protected void Page_Load(object sender, EventArgs e)
    {
        test = "Hello lesley";
        Label label = new Label();
        label.Text = test;
        Page.Controls.Add(label);
    }

Comments

2

Alternatively, if you already have that property in your index.aspx.cs, you can do this in index.aspx html page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
</head>
    <body>
        <form id="form1" runat="server">
        Hi <b><%=test%></b>
        </form>
    </body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.