Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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> 
like image 231
David Mulder Avatar asked Mar 16 '11 21:03

David Mulder


People also ask

How do you get the label value in code behind?

While you can get the label text on the post-back, it will be the same text as when it was set, thus the ''. Set its VALUE in the javascript, then in the code-behind: string hf = myHidden.

What is code behind page in asp net?

Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic.

How do I add a line break to a label in asp net?

The break tags will be rendered as text, rather than applying a carriage return. You can wrap your asp label in a pre tag, and it will display with whatever line breaks are set from the code behind. Show activity on this post. You can also use <br/> where you want to break the text.


2 Answers

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?

like image 152
Oded Avatar answered Oct 01 '22 22:10

Oded


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"; 
like image 29
Tejs Avatar answered Oct 01 '22 22:10

Tejs