如何从页面加载后的代码中设置ASP.NET标签文本? [英] How do I set an ASP.NET Label text from code behind on page load?
问题描述
我似乎找不到答案.这是场景: 我有一个使用C#的ASP.NET项目.我正在使用C#(使用ADO)从sqlite数据库加载数据(用户名,电子邮件等).我将数据加载到App_Data文件夹中的类文件中的静态全局变量中.在加载期间,我需要能够将用户名插入页面上的ASP.NET标签中.
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.
在PHP中,我会这样做:
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>
谁能解释这是怎么做的?我是ASP.NET的新手.
Can anyone explain how this is done? I'm new to ASP.NET.
这是一些更详细的更新.我尝试了你们的建议.我的页面加载功能位于一个名为RankPage.aspx.cs的文件中,而其下面的表位于RankPage.aspx中.这个想法是列出我从数据库中检索到的一堆用户.我投入了"myLabel"只是为了对其进行测试.现在,在未在我的代码中声明"myLabel"的情况下,错误提示"myLabel"在当前上下文中不存在.如果使用FindControl()函数声明"myLabel",则会收到运行时异常,即"myLabel"未设置为对象的实例.
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.
这是代码:
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>
推荐答案
对于此标签:
<asp:label id="myLabel" runat="server" />
在使用(C#)的代码中:
In the code behind use (C#):
myLabel.Text = "my text";
更新(以下是更新的问题):
Update (following updated question):
您无需使用FindControl
-整个行都是多余的:
You do not need to use FindControl
- that whole line is superfluous:
Label myLabel = this.FindControl("myLabel") as Label;
myLabel.Text = "my text";
应该只是:
myLabel.Text = "my text";
Visual Studio设计人员应该创建一个文件,其中所有服务器端控件均已正确添加到类中(默认情况下在RankPage.aspx.designer.cs
文件中).
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).
您正在谈论的是RankPage.cs
文件-Visual Studio将其命名为RankPage.aspx.cs
的方式.您如何将这些文件链接在一起?
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?
这篇关于如何从页面加载后的代码中设置ASP.NET标签文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!