如何在页面加载后从代码设置ASP.NET标签文本?
|
我似乎找不到答案了.这是场景:
在PHP中,我只是这样做: <?php
function GetUserName() {
//code which retrieves username from db.
return username;
}
?>
<p>Here is the username: <?php echo GetUserName(); ?></p>
任何人都可以解释这是怎么做到的?我是ASP.NET的新手. 这里有一些更多的细节.我试过你们建议的我的页面加载功能在一个名为RankPage.aspx.cs的文件中,下面的表在RankPage.aspx中.这个想法是列出我从数据库检索的一堆用户.我投入’myLabel’来测试它.现在,没有在我的代码中声明’myLabel’,它的错误’myLabel’不存在于当前的上下文中.如果我使用FindControl()函数声明“myLabel”,我会得到一个运行时异常,“myLabel”未设置为对象的实例. 以下是代码: 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#): myLabel.Text = "my text"; 更新(更新后的问题): 您不需要使用FindControl – 整行是多余的: Label myLabel = this.FindControl("myLabel") as Label;
myLabel.Text = "my text";
应该是: myLabel.Text = "my text"; Visual Studio设计器应该创建一个文件,其中所有服务器端控件已经正确添加到该类(默认情况下在RankPage.aspx.designer.cs文件中). 你正在谈论一个RankPage.cs文件 – Visual Studio将命名它的方式是RankPage.aspx.cs.如何将这些文件链接在一起? (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 尝试创建类型为’TypeNewsContro
- 当使用SignalR和传输模式长轮询时,Asp.net会话永
- asp.net – URL长度的最佳限制是什么? 100,200
- asp.net-mvc-3 – MVC3重定向到ajax调用后的动作
- asp.net-mvc – 哪个更快asp.net mvc json或json
- asp.net – 实体框架Web配置文件
- 身份验证 – 如何仅为ASP.NET 5中的受保护操作添
- asp.net-mvc-3 – StructureMap初学者|物业注入
- asp.net-mvc – 在哪里托管我的MVC4应用程序?
- 如何在asp.net会员中手动更改密码?
