开发者

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>


For this label:

<asp:label id="myLabel" runat="server" />

In the code behind use (C#):

myLabel.Text = "my text"; 

Update (following updated question):

You do not need to use FindControl - that whole line is superfluous:

  Label myLabel = this.FindControl("myLabel") as Label;
  myLabel.Text = "my text";

Should be just:

  myLabel.Text = "my text";

The Visual Studio designer should create a file with all the server side controls already added properly to the class (in a RankPage.aspx.designer.cs file, by default).

You are talking about a RankPage.cs file - the way Visual Studio would have named it is RankPage.aspx.cs. How are you linking these files together?


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


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


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>


In the page load event you set your label

lbl_username.text = "some text";


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 :)


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";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜