【问题标题】:How can I data bind <%# Eval("Object.Property")%> programmatically in VB.NET如何在 VB.NET 中以编程方式绑定 <%# Eval("Object.Property")%>
【发布时间】:2011-01-07 13:25:56
【问题描述】:

我可以在设计时在 ASP.NET 网格视图中使用以下设置将内部对象属性绑定到网格视图

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

但我想知道的是在运行时以编程方式创建它

即定义我的列,将它们添加到gridview,然后数据绑定

【问题讨论】:

    标签: asp.net vb.net data-binding


    【解决方案1】:

    我不确定这是否是您想要实现的目标。但是,如果您想在运行时根据对象的属性动态创建列,请查看以下代码(示例使用 BoundColumns,当您需要 TemplateColumns 时请查看 Volpa 的答案):

    ASPX:

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

    代码隐藏:

    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
    

    【讨论】:

      【解决方案2】:

      一种方法是创建一个实现ITemplate 接口的类:

      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));
          }
      }
      

      然后在您的代码隐藏中分配 ItemTemplate 如下:

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

      如果您的自定义模板位于单独的 .ascx 文件中,另一种方法是使用 Page.LoadTemplate

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

      .ascx 文件将如下所示:

      <%# Eval("MyProperty") %>
      

      【讨论】:

        猜你喜欢
        • 2014-08-18
        • 1970-01-01
        • 2021-06-18
        • 2012-12-03
        • 2011-10-22
        • 1970-01-01
        • 2011-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多