Turn a string into a control name

  • Thread starter Web Team @ Borough of Poole
  • Start date
W

Web Team @ Borough of Poole

Hi All,

I have this code, which I am trying not to replicate many times in my
code-behind.

Me.img1.Visible = False
Me.img1UploadButton.Visible = True
Me.pnlImg1Uploader.Visible = True
Me.img1Cancel.Visible = True

I have mulitple controls on the page which do similar things. They are
all named in a consistant manner. e.g: img1, img2, img3...etc. I would
like to call one function to carry out the above code, but I am not
sure how to get the ID into the code. I guess something like this:
me.img+TheID+.Visible = False (Obviously that wont work, but should
give you an idea of what I'm trying to achieve)

Any help/suggestions will be appreciate.

Many Thanks.
 
N

Nathan Sokalski

You can use the FindControl method as follows:

Private Sub ShowHide(ByVal index As Integer)

CType(Me.FindControl("img" & CStr(index)),
System.Web.UI.WebControls.Image).Visible = False

End Sub


The parameter for the FindControl method is a String, which is the ID of the
control you want. It sounds like you can figure everything else out from
here, if you have any questions feel free to ask. Good Luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top