从C#中的ID获取标签文本值 [英] Get label text value from ID in C#

查看:111
本文介绍了从C#中的ID获取标签文本值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C#中的ID获取标签文本值

首先给出示例

在正文标签内

how to Get label text value from ID in C#

Let give the example first

Inside the body tag

<asp:Label id="Label1" runat="server" Text="Hello world"/>



在脚本标签内使用c#



Inside the script tag using c#

void Page_Load(Object Source, EventArgs E)<br />{<br />     String test = Label1.Text<br />}



确定,那是正常方法.但是我的问题是我有很多Label.让我说Label1直到Label10,然后我想得到使用循环的标签值

示例:希望这种代码存在,请注意,getLabelID不是功能,甚至不存在,它只是可以根据其ID(其中参数是字符串)获得Label的东西.这东西存在吗?如果没有人可以解决?




OK, thats the normal method.
But my problem is I have many Label.
Lets say Label1 until Label10 and I want to get the label value by using loop

Example: hope this kind of code exist, note that getLabelID is not a funtion or even exist, its just something that can get the Label based on its id where the parameter is a string. Is this thing exist?, if not any one have a solution?

void Page_Load(Object Source, EventArgs E)<br />{<br />     for(int aa=1; aa<=10; aa++)<br />     {<br />          String test = getLabelID("Label" + aa.toString()).Text;<br />     }<br />}

推荐答案

asipo写道:

这东西存在吗?


是... FindControl(),但是您必须将其转换为Label,例如(Label)FindControl()


yes...FindControl() but you have to convert it to Label e.g (Label)FindControl()


这篇关于从C#中的ID获取标签文本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆