hi

i am joshi. i got a problem in my project, ie i have declared 50 labels with a name q1,q2....q50 now i want to call these labels with a use of for loop..... but how

please help me....

hii,
iam giving an example to display dynamically two labels.hope it is usefull.
if u have already have 5 labels in u r page but u want to display only 2 labels ,then this function is usefull
label1,label2 be u r label ids in the page

dim lbl as string
dim l as label
for 1=0 to 1
lbl="label"& convert.tostring(i)
l=page.findcontrol(lbl)
l.visible=true
next

hi
thq for your reply...
if i wrote l=page.findcontrol(lbl), i am getting an error that Error Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.Label'. An explicit conversion exists (are you missing a cast?)

how can i slove this problem
please reply me.....

hii,
did u declare l as label
dim l as label
thats the only reason i see for getting the error.the code i gave u was being used by me in my project .

hii,
i have figured it out.since u have declared starting from q1 ,the code i gave works from qo,so here is the correct code
Dim i As Integer
Dim lbl As Label
Dim b As String
For i = 0 To 1
b = "label" & Convert.ToString(i + 1)
lbl = Page.FindControl(b)
lbl.Visible = True
Next

hi
thanks for your reply ones again..
i have wrote same code but i am getting an error that "object reference is not set to an instance"

You should not have to find the labels unless they are nested in another control like a datalist, grid, or repeater, etc. Try telling us what you want to do in this loop.

Oh, and the reason why his code isn't working for you is due to a few flaws. The below code is suited for you:

Public Function LoopingTheLbls()
Dim i As Integer
Dim lbl As Label
Dim b As String
For i = 0 To 49
b = "q" & Convert.ToString(i + 1)
lbl = Page.FindControl(b)
lbl.Text = "Help I am control " & (i + 1)
next 
End Function
This article has been dead for over six months. Start a new discussion instead.