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

前端 未结 7 1746
终归单人心
终归单人心 2020-12-03 07:39

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 datab

7条回答
  •  被撕碎了的回忆
    2020-12-03 07:45

    For this label:

    
    

    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?

提交回复
热议问题