How to set databinding.eval in code?

  • Thread starter Thread starter Sue
  • Start date Start date
S

Sue

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

<asp:Label runat="server" Text='<%# DataBinder.Eval
(Container, "DataItem.Somefield") %>'></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
 
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_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
Dim lb As Label = e.Item.FindControl("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,
 
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?

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
 
Back
Top