How to set databinding.eval in code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sue

    #1

    How to set databinding.eval in code?

    Is there a way to take datagrid .aspx like this:

    <asp:Label runat="server" Text='<%# DataBinder.Eval
    (Container, "DataItem.Somef ield") %>'></asp:Label>

    and set it in code like this:

    sub setMyLabel()
    MyLabel Label = new Label
    With Me.MyLabel
    .ID = "SomeID"
    .???
    End With
    End Sub

    and end up with the same functionality?

    tia,
    Sue
  • Parker Zhang [MSFT]

    #2
    RE: How to set databinding.eva l in code?

    Hi Sue,

    I am not very clear about what you are trying to do.

    1. Do you have any special requirement to bind the label to a column
    dynamically?
    2. ItemDataBound fires for each record. That means your code bind the label
    to a column many time.
    3. Why not bind the label at design time and change its properties
    dynamically?

    Private Sub DataGrid1_ItemD ataBound(ByVal sender As Object, ByVal e As
    System.Web.UI.W ebControls.Data GridItemEventAr gs) Handles
    DataGrid1.ItemD ataBound
    If e.Item.ItemType = ListItemType.It em Then
    Dim lb As Label = e.Item.FindCont rol("Label1")
    If Not lb Is Nothing Then
    Dim rd As New System.Random()
    Dim r As Integer = rd.Next(255)
    Dim g As Integer = rd.Next(255)
    Dim b As Integer = rd.Next(255)
    lb.ForeColor = Color.FromArgb( r, g, b)
    End If
    End If
    End Sub

    HTH,

    --
    Parker Zhang
    Microsoft Developer Support

    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • Sue

      #3
      RE: How to set databinding.eva l in code?

      >I am not very clear about what you are trying to do.[color=blue]
      >
      >1. Do you have any special requirement to bind the label[/color]
      to a column[color=blue]
      >dynamically?[/color]

      Hi Parker: My intent in the end, is to keep the aspx page
      from being overwhelmed by hundreds of lines of code
      needed to declare an object, set attributes, bind-data,
      etc. If I can set object attributes/data-binding as much
      as possible programatically , then simply declare the
      object in the .aspx page, it would make things much
      cleaner and easier to work with. Ultimately I'd like to
      have each column in it's own class. The ultimate goal is
      to be able to put and pull class objects on a aspx page
      as needed.

      I'm open to suggestions as to how to accomplish these
      goals. My shop is new to VB.NET and we're still trying to
      get our arms around it and establish best practices for
      the dept.

      Does this help or confuse?

      Sue

      Comment

      Working...