Ledgemonkey
Ledgemonkey

Reputation: 7481

How can I display "hello world" in an .aspx file?

I have Default.aspx and a seperate code file:

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       public string hello = "hello world"
   }
}

I would like to display this in my Default page i have tried to use <%=hello%> but does not work. What am I doing wrong?

Upvotes: 3

Views: 18334

Answers (6)

Pranay Rana
Pranay Rana

Reputation: 176936

also try out

aspx page

<%= this.hello%>

.cs codebehind file

public partial class _Default : System.Web.UI.Page
{
    public string hello = "hello world";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

just write this will do the task

Response.Write("Hello World");

Upvotes: 3

Kyle Preiksa
Kyle Preiksa

Reputation: 514

You need to actually write this to the markup. You can do this by creating a label(or literal): <asp:Label ID="helloLabel" runat="server" Text = "<%#HelloWorld()%> ></asp:Label> Then you will need a function called HelloWorld which returns a string

private string HelloWorld()
{
    string hello = "Hello World";
    return hello;
}

or you could set the label text directly from a function.

helloLabel.Text = "Hello World";

Upvotes: 3

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Try this:

public partial class _Default : System.Web.UI.Page
{
    public string hello = "hello world";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Upvotes: 4

codingbiz
codingbiz

Reputation: 26386

Use a label instead. You will be able to format the output easily

 in your .aspx
 <body>
   <asp:Label runat="server" ID="HelloLabel"></asp:Label>
 </body>

//code behind
protected void Page_Load(object sender, EventArgs e)
{
  string hello = "i need more practice";
  HelloLabel.Text = "hello";
}

Upvotes: 3

Thinking Sites
Thinking Sites

Reputation: 3542

Your code won't compile the way it is. Try this:

public partial class _Default : System.Web.UI.Page
{
    public string Hello { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        Hello = "hello world";
    }
}

Upvotes: 3

Shyju
Shyju

Reputation: 218792

 protected void Page_Load(object sender, EventArgs e)
 {
   string hello = "i need more practice";
   Response.Write(hello);
 }

Upvotes: 4

Related Questions