Lesley Peters
Lesley Peters

Reputation: 409

asp.net show string from a class on html page

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?

Upvotes: 2

Views: 913

Answers (3)

ken lacoste
ken lacoste

Reputation: 894

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>

Upvotes: 2

Nikson K John
Nikson K John

Reputation: 461

Try this

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

Upvotes: 2

Izzy
Izzy

Reputation: 6866

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

Upvotes: 4

Related Questions