DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   reg OnlineExam Project (http://www.daniweb.com/forums/thread91989.html)

joshireddy.p Oct 7th, 2007 2:18 am
reg OnlineExam Project
 
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....

greeny_1984 Oct 8th, 2007 2:43 am
Re: reg OnlineExam Project
 
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

joshireddy.p Oct 9th, 2007 1:33 am
Re: reg OnlineExam Project
 
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.....

greeny_1984 Oct 9th, 2007 2:08 am
Re: reg OnlineExam Project
 
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 .

greeny_1984 Oct 9th, 2007 3:59 am
Re: reg OnlineExam 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

joshireddy.p Oct 10th, 2007 1:29 am
Re: reg OnlineExam Project
 
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"

SheSaidImaPregy Oct 10th, 2007 2:47 am
Re: reg OnlineExam Project
 
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

joshireddy.p Oct 14th, 2007 1:24 am
Re: reg OnlineExam Project
 
tq
i got it.
i know what the mistake i am doing.
thq ones again


All times are GMT -4. The time now is 1:21 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC