Eval in ASP.net / VB.net or a better way?

Discussion in 'C#' started by mjamesb, Sep 14, 2006.

  1. #1
    How do I evaluate a string that represents and object name as the object. I Know how to do this in a couple languages but not ASP.Net/VB.net

    I have 18 user controls on a page the controls are called Time1, Time2, Time3 etc... When the page loads I want to be able to set properties of them dynamically using a loop.

    This code is wrong but this is the jist of what I want. I cant find an eval or evaluate function. Basically want it to evaluate the string to the control like Time1.Hour = Request("Time1Hour")

    Dim i As Integer
    For i = 1 To 18
    If Request("Time" & i & "Hour") <> Nothing Then
    eval("Time" & i & ".hour") = Request("Time" & i & "Hour")
    End If
    Next

    Any Idea? Is there another way to reference the controls on the page? This is the first time I have used custom server controls and I don't want to use the viewstate postback crap.
     
    mjamesb, Sep 14, 2006 IP
  2. mjamesb

    mjamesb Member

    Messages:
    88
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    48
    #2
    Ok..was being lazy before but found what I needed to get the control by a string that represented the controlid...if anyone is interested...

    me.FindControl(id as string)

    Dim i As Integer
    Dim T As TimeSelector
    For i = 1 To 18
    If Request("Time" & i & "Hour") <> Nothing Then
    T = Me.FindControl("Time" & i)
    T.Hour = Request("Time" & i & "Hour")
    End If
    Next
     
    mjamesb, Sep 14, 2006 IP
  3. Froggie

    Froggie Well-Known Member

    Messages:
    665
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    120
    #3
    you should really be doing enumuration of the controls on the page

    dim ctrl as Control
    For Each ctrl in Page.Controls
    if ctrl.GetType() == typeof(Timer) then
    'do something
    end
    next

    look at how to do a for each statement in vb.net and the collection of the controls is in Page.Controls
     
    Froggie, Sep 14, 2006 IP
  4. mjamesb

    mjamesb Member

    Messages:
    88
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    48
    #4
    Cool...yeah that is more generic....I guess I will start to delve into the UI stuff more in .Net. Up to this point most of the stuff I have done is back-end and when doing front end just using traditional methods (no controls, using url variables, regular form posts)
     
    mjamesb, Sep 14, 2006 IP