Click here to Skip to main content
16,022,798 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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"/>


Inside the script tag using c#
void Page_Load(Object Source, EventArgs E)<br />{<br />     String test = Label1.Text<br />}



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 />}
Posted

1 solution

asipo wrote:
Is this thing exist?

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

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900