Results 1 to 5 of 5

Thread: eval a string into an object

  1. #1

    Thread Starter
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292

    eval a string into an object

    say I have a textbox with attribute id="test_1"

    How could I reference this id if I am in a loop

    ex.
    Code:
    for i as Integer = 0 to 5
    ' I would like to give this textbox a value, I thought I could do something like
       Eval("test_" & i).Value = "someValue"
    ' but that did not work, I also tried to do 
      dim tmpTB as Textbox = Ctype("test_" & i, TextBox)
        
    Next
    Is there a way to do this?

    bsw

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: eval a string into an object

    If the text box is just on the page, it's just like normal application development: test_1.Text="....."

    If it's within another object, such as a GridView, you need to do a FindControl for it:
    Dim myTxt as TextBox = GridView1.FindControl("test1")
    myTxt.Text="......"

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: eval a string into an object

    Quote Originally Posted by bsw2112
    say I have a textbox with attribute id="test_1"

    How could I reference this id if I am in a loop

    ex.
    Code:
    for i as Integer = 0 to 5
    ' I would like to give this textbox a value, I thought I could do something like
       Eval("test_" & i).Value = "someValue"
    ' but that did not work, I also tried to do 
      dim tmpTB as Textbox = Ctype("test_" & i, TextBox)
        
    Next
    Is there a way to do this?

    bsw
    In order to do this, you have to put the textboxes into a collection (array, list, arraylist...) then looping thru the collection.

  4. #4

    Thread Starter
    Hyperactive Member bsw2112's Avatar
    Join Date
    Nov 2001
    Location
    ottawa, canada
    Posts
    292

    Re: eval a string into an object

    Thanks timeshifter and stanav


    timeshifter, I went with your approach which resulted in this code
    Code:
     
    If PlanData.Rows.Count > 0 Then
                    For i As Integer = 0 To PlanData.Rows.Count - 1
                        Dim tmpDTB As New DoubleTextBox
                        tmpDTB = form1.FindControl("milestone2008_" & i + 1)
                        tmpDTB.Value1 = PlanData.Rows(i).Item(0)
                        tmpDTB.Value2 = GeneralUtilites.ConvertDate(PlanData.Rows(i).Item(1), "CAN")
                    Next
    
                End If
    instead of this code which was crazy. form1.FindControl is a blessing

    Code:
    If PlanData.Rows.Count > 0 Then
    Select Case (PlanData.Rows.Count)
                        Case 1
    
                            milestone2008_1.Value1 = PlanData.Rows(0).Item(0)
                            milestone2008_1.Value2 = GeneralUtilites.ConvertDate(PlanData.Rows(0).Item(1), "CAN")
    
                        Case 2
                            milestone2008_1.Value1 = PlanData.Rows(0).Item(0)
                            milestone2008_1.Value2 = GeneralUtilites.ConvertDate(PlanData.Rows(0).Item(1), "CAN")
    
                            milestone2008_2.Value1 = PlanData.Rows(1).Item(0)
                            milestone2008_2.Value2 = GeneralUtilites.ConvertDate(PlanData.Rows(1).Item(1), "CAN")
    
    Case X                   
    etc....
    
                    End Select
    End If
    stanav
    In order to do this, you have to put the textboxes into a collection (array, list, arraylist...) then looping thru the collection.
    how do I go about putting controls from my page to a collection that I can then manipulate which would alter the value of the actual control on the page.

    In other words say I have the 3 textboxes bellow.
    Code:
    <input name="foo1" id="foo1" /><br />
    <input name="foo2" id="foo2" /><br />
    <input name="foo3" id="foo3" /><br />
    How would I add these three boxes to a collection and then use something like collection.value to make the three like so

    <input name="foo1" id="foo1" value="foo1value" /><br />
    <input name="foo2" id="foo2" value="foo2value" /><br />
    <input name="foo3" id="foo3" value="foo3value" /><br />

    bsw
    Last edited by bsw2112; May 31st, 2007 at 03:59 PM.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: eval a string into an object

    You don't need to. You can loop through the collection. If it's sitting in a table, div or simply the page, you can iterate through the .Controls collection of the object and test its type.

Posting Permissions

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



Click Here to Expand Forum to Full Width