Dean
Dean

Reputation: 5946

How can I data bind <%# Eval("Object.Property")%> programmatically in VB.NET

I can bind a inner object property to gridview using the following setup at design time in an ASP.NET gridview

<asp:TemplateField HeaderText="ObjectName" >
                            <ItemTemplate>                                        
                                            <%# Eval("Object.property")%>                                       
                                    </ItemTemplate>
                        </asp:TemplateField>

but what I would like to do know is create this programmatically at runtime

i.e. define my columns, add them to the gridview and then databind

Upvotes: 0

Views: 5137

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460168

I'm not sure if this is what you want to achieve. But if you want to create columns dynamically according to your object's properties on runtime, have a look at following code(example is with BoundColumns, have a look at Volpa's answer when you need TemplateColumns):

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"></asp:GridView>

Codebehind:

Public Partial Class GridViewTest
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData()
        End If
    End Sub

    Private Sub BindData()
        Dim cList As New List(Of CustomClass)
        For i As Int32 = 1 To 10
            Dim c As New CustomClass
            c.ID = i
            c.Name = "Object " & i
            cList.Add(c)
        Next
        Dim model As New CustomClass
        For Each prop As Reflection.PropertyInfo In model.GetType.GetProperties
            If prop.CanRead Then
                Dim field As New BoundField()
                field.HeaderText = prop.Name
                field.DataField = prop.Name
                Me.GridView1.Columns.Add(field)
            End If
        Next
        Me.GridView1.DataSource = cList
        Me.GridView1.DataBind()
    End Sub

End Class

Public Class CustomClass
    Private _id As Int32
    Private _name As String

    Public Property ID() As Int32
        Get
            Return _id
        End Get
        Set(ByVal value As Int32)
            _id = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
End Class

Upvotes: 2

volpav
volpav

Reputation: 5128

One way would be to create a class that implements ITemplate interface:

public class PropertyTemplate : ITemplate
{
    private string _value = string.Empty;

    public PropertyTemplate(string propValue) 
    { 
        this._value = propValue;
    }

    public void InstantiateIn(Control container)
    {
        container.Controls.Add(new LiteralControl(this._value));
    }
}

Then in your code-behind assing the ItemTemplate as following:

myTemplateField.ItemTemplate = new PropertyTemplate(myBusinessObject.MyProperty);

Another way would be to use Page.LoadTemplate if your custom template resides in the separate .ascx file:

myTemplateField.ItemTemplate = Page.LoadTemplate("~/MyTemplate.ascx");

And the .ascx file will look like:

<%# Eval("MyProperty") %>

Upvotes: 1

Related Questions