Turn a string into a control name

Discussion in 'ASP .Net' started by Web Team @ Borough of Poole, Sep 2, 2005.

  1. 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.
     
    Web Team @ Borough of Poole, Sep 2, 2005
    #1
    1. Advertisements

  2. 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!
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Web Team @ Borough of Poole" <> wrote in message
    news:...
    > 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.
    >
     
    Nathan Sokalski, Sep 3, 2005
    #2
    1. Advertisements

  3. Excellent - Thank you! :)
     
    Web Team @ Borough of Poole, Sep 3, 2005
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. feng
    Replies:
    1
    Views:
    10,121
    Martha[MSFT]
    Feb 28, 2004
  2. MattC
    Replies:
    2
    Views:
    491
    Andrea Desole
    Oct 20, 2005
  3. Stewart Midwinter

    How to turn a variable name into a string?

    Stewart Midwinter, Mar 11, 2005, in forum: Python
    Replies:
    11
    Views:
    936
    Scott David Daniels
    Mar 12, 2005
  4. Xah Lee

    turn text lines into a list

    Xah Lee, Jun 27, 2005, in forum: Python
    Replies:
    8
    Views:
    628
    Big and Blue
    Jun 28, 2005
  5. Replies:
    2
    Views:
    720
  6. Eric Osman
    Replies:
    2
    Views:
    591
    Eric Osman
    Apr 14, 2004
  7. PerlFAQ Server
    Replies:
    0
    Views:
    172
    PerlFAQ Server
    Jan 30, 2011
  8. PerlFAQ Server
    Replies:
    0
    Views:
    139
    PerlFAQ Server
    Mar 24, 2011
Loading...