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. Advertising

  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. Advertising

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

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:
    8,723
    Martha[MSFT]
    Feb 28, 2004
  2. Stewart Midwinter

    How to turn a variable name into a string?

    Stewart Midwinter, Mar 11, 2005, in forum: Python
    Replies:
    11
    Views:
    721
    Scott David Daniels
    Mar 12, 2005
  3. Eric Osman
    Replies:
    2
    Views:
    355
    Eric Osman
    Apr 14, 2004
  4. PerlFAQ Server
    Replies:
    0
    Views:
    94
    PerlFAQ Server
    Jan 30, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    82
    PerlFAQ Server
    Mar 24, 2011
Loading...

Share This Page