Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Refer to an object through a variable...

  1. #1
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    203
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default Refer to an object through a variable...

    with .net can you refer to an asp form object from a variable or part variable.

    an example would be with flash you are able to refer to variables or movies clips with the following syntax

    _root.["mymovie" + i]_alpha = 100

    can i do something in vb.asp to refer to an object in a similar way?

    and if you can, does this have a technically title!?

  2. #2
    Join Date
    Jun 2005
    Posts
    1,080
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    I haven't tried this in VB, but have used it in Javascript. I did a quick Google, and think this thread should help:

    http://forums.devx.com/archive/index.php/t-71914.html

  3. #3
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    203
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default

    Cheers, I'll take a look.

  4. #4
    Join Date
    Jan 2006
    Posts
    419
    Thanks
    2
    Thanked 16 Times in 16 Posts

    Default

    In asp.net you can refer to a form variable (post) using request.form("fieldname") or (get) using request.querystring("fieldname") or (post and get) using request.params("fieldname").

    Although it may not be necessary to do the above as if your controls on the asp.net page are either html or web controls with the runat=server attribute you can reference the value of the control programatically. This is the prefered method.

  5. #5
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    203
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default

    what i'm after is being able to refer to a number of asp.net fields within a loop, for example... (no syntax)


    do while i<10

    mytextfield[i].text = db("field_value")

    loop


    can this be done with dot net?

  6. #6
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Can't you use an array or dictionary/collection? To store the variables?
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  7. #7
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

  8. #8
    Join Date
    Jun 2005
    Posts
    1,080
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    Try something like this:

    Code:
     
    do while i < 10
     
        dim myTextBox as TextBox
        myTextBox = Page.FindControl("textbox" + i)
     
        myTextBox.Text = db("Field_Value")
     
    loop
    You could optimize this a bit by putting all the textbox controls in a placeholder and using FindControl within the placeholder such that the code won't have to look through all the controls on the page.

  9. The Following User Says Thank You to Sol For This Useful Post:

    RFH Reseller: creativeworks (20th July 2007)

  10. #9
    Join Date
    Jun 2005
    Location
    Tunbridge Wells, Kent
    Posts
    203
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Default

    haven't tried yours yet Sol, but how does it know that's being refered to from a variable rather than an element name?

  11. #10
    Join Date
    Jun 2005
    Posts
    1,080
    Thanks
    4
    Thanked 15 Times in 15 Posts

    Default

    It's the line:

    Code:
    myTextBox = Page.FindControl("textbox" + i)
    Page.Findcontrol goes through all the controls within the Page (note that this is not recursive, so if you have a PlaceHolder that holds the textboxes it won't go through that). It looks at the name of each control and compares it to "textbox" + i, and if a match is found it assigns it to that myTextBox variable, so that you can access all the properties etc.

    Depending on what exactly you are doing, you could dynamically inject the textboxes to the Page.

    This of course runs server side, as if you were doing this client-side I would recommend Javascript and the 'Eval' function.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. PHP Data Object
    By nick in forum Community Feedback and Suggestions
    Replies: 5
    Last Post: 29th November 2006, 05:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •