[asp.net] How do I set an ASP.NET Label text from code behind on page load?

I can't seem to find an answer out there for this. Here's the scenario: I have an ASP.NET project using C#. I'm loading data (Username, email, etc...) from a sqlite database with C# (using ADO). I'll be loading the data into static Global variables in a class file in my App_Data folder. I need to be able to insert the username into an ASP.NET Label on a page during load.

In PHP, I would just do it like this:

<?php
function GetUserName() {
//code which retrieves username from db.
return username;
}
?>
<p>Here is the username: <?php echo GetUserName(); ?></p>

Can anyone explain how this is done? I'm new to ASP.NET.

Here's an update for some more detail. I tried what you guys have suggested. My page load function is in a file called RankPage.aspx.cs and the table below it is in RankPage.aspx. The idea is to list a bunch of users that I've retrieved from the database. I threw in 'myLabel' just to test it. Right now, without declaring 'myLabel' in my code behind, it errors that 'myLabel' does not exist in the current context. If I declare 'myLabel' using the FindControl() function, I get a runtime exception that 'myLabel' isn't set to an instance of an object.

Here's the code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Label myLabel = this.FindControl("myLabel") as Label;
        myLabel.Text = "my text";
    }
}

<table>
    <tbody>
        <tr>
            <th>Name</th>
            <th>Score</th>
        </tr>

        <tr>
            <td>name</td>
            <td>Score</td>
        </tr>

        <!-- Current User -->
        <tr>
            <td><asp:Label id="currentUserName" runat="server"></asp:Label></td>
            <td><asp:Label id="currentUserScore" runat="server"></asp:Label></td>
            <td><asp:Label ID="myLabel" runat="server" /></td>
        </tr>
        <!-- End Current User -->

    </tbody>
</table>

This question is related to asp.net label

The answer is


In the page load event you set your label

lbl_username.text = "some text";


Try something like this in your aspx page

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

and then in your codebehind you can just do

myLabel.Text = "My Label";

In your ASP.NET page:

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

In your code behind (assuming you're using C#):

function Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       UserNameLabel.Text = "User Name";
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    myLabel.Text = "My text";
}

this is the base of ASP.Net, thinking in controls, not html flow.

Consider following a course, or reading a beginner book... and first, forget what you did in php :)


I know this was posted a long while ago, and it has been marked answered, but to me, the selected answer was not answering the question I thought the user was posing. It seemed to me he was looking for the approach one can take in ASP .Net that corresponds to his inline data binding previously performed in php.

Here was his php:

<p>Here is the username: <?php echo GetUserName(); ?></p>

Here is what one would do in ASP .Net:

<p>Here is the username: <%= GetUserName() %></p>

If you are just placing the code on the page, usually the code behind will get an auto generated field you to use like @Oded has shown.

In other cases, you can always use this code:

Label myLabel = this.FindControl("myLabel") as Label; // this is your Page class

if(myLabel != null)
   myLabel.Text = "SomeText";