开发者

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

开发者 https://www.devze.com 2023-02-02 19:52 出处:网络
I can bind a inner object property to gridview using the following setup at design time in an ASP.NET gridview

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


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


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") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消