Cast a string to a name of a web label
HI, using vs2008 and building a web app. On a asp page called blackjack.aspx, I have four la开发者_如何学Cbels with id of lbBJTStatusP1 lbBJTStatusP2 lbBJTStatusP3 lbBJTStatusP4.
I want to address those labels in a single sub by casting the casting two strings into the control name, so that string lbBJTStatusP & "1" would refer to lbBJTStatusP1.This is done on the code behind page.
So far I have tried this but with no success. boxct refers to either "1" "2" "3" or "4".
DirectCast(blackjack.Controls.Find("lbBJTStatusP" & boxct, True)(0), Label).BackColor = stoodcolor
Can it be done and if so how. Thanks for all and any help.
You can't "cast" a string to a specific instance of a control.
What you can do is use FindControl: that accepts a string, searches (one level deep, not more) for a control with that name and returns it. The method returns a Control
, so you might need to cast it to Label
.
I have labels named lblqu01ex - lblqu10ex. I set the text value through coding as follows.
for i = 1 to 10
ex = "lbl" & IIf(i = 10, "qu10", "qu0" & i) & "ex"
DirectCast(FindControl(ex), Label).Text = 2*100/i
next
Its work.
精彩评论