Link to home
Start Free TrialLog in
Avatar of jaysch
jayschFlag for United States of America

asked on

Assigining the same value to multiple label controls

Rather than do this to assign the same value to several lables on the same page:
            lblPropName1.Text = strPropertyName
            lblPropName2.Text = strPropertyName
            lblPropName3.Text = strPropertyName
            lblPropName4.Text = strPropertyName

Can i do something like this instead?

Dim i As Integer
            For i = 0 To 3
                lblPropName(i).text = strPropertyName
            Next
Avatar of jppinto
jppinto
Flag of Portugal image

Try like this.

jppinto
For Each ctl As Control In form1.Controls
   If TypeOf ctl Is LabelThen
       ctl.Text= strPropertyName
   End If
Next

Open in new window


For Each ctl As Control In form1.Controls
   If TypeOf ctl Is Label Then
       ctl.Text= strPropertyName
   End If
Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of vladimir_kalashnikov
vladimir_kalashnikov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaysch

ASKER

jppinto,

I'm getting an error stating that form1 is not declared. I'm using Master pages, could this be the problem?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vladimir_kalashnikov
vladimir_kalashnikov

The problem with jppinto's method is that it will update ALL labels in the panel (or whatever container you are using).  This may work in your case, but I wouldn't recommend it in general.  You will also have to make the panel a server control.
Avatar of jaysch

ASKER

Unfortunately, no method suggested so far seems to be working. Could the problem have to do with the use of Master pages and the Update panel. The code can't find the controls? Any other ideas?

Thanks for the feedback.
I also use master pages and have an Update Panel on my page. I've tryed the last solution that I posted and it's working. Did you've change Your_UpdatePanel for the name of your Update Panel?
Avatar of jaysch

ASKER

Yes, I modified the Update panel name.
SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are you still getting an error, are you getting no errors but no updates, are you getting partial updates?

Also, to make sure only the controls you are interested in changing will change, you may want to put them in a collection or in a separate sup panel and then iterate the controls in that panel, like already suggested.
Avatar of jaysch

ASKER

No luck so far. On the last example above, 'YourFormName.Controls(strLabel & i) = strPropertyName'

I've tried:

Updatepanel1.Controls(strLabel & i) = strPropertyName

but I receive a "Property Item is Read Only" error in intellesense.

I can't but the four label controls in sub-panels because they are spread out all over the page and because they exist within Wizard Steps. Can you explain in greater detail how to put these controls in a collection?

Thanks
You'll definitely have to make the labels not Read Only to be able to edit them.
Avatar of jaysch

ASKER

How do you make a label not Read Only? Doesn't seem to have a read only property associated with it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaysch

ASKER

These solutions were able to get me pointed in the right direction. Thanks to all for your help.