【问题标题】:Read data from DataTable - ASP.NET从 DataTable 读取数据 - ASP.NET
【发布时间】:2015-06-26 06:11:50
【问题描述】:

我正在使用带有 VB.net 的 ASP.net 4.5。 (C#例子也可以,我会转换)

我有以下代码,用于在我的表单上填充中继器控件。

Private pgdArticles As New PagedDataSource

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        pgdArticles = GetData()
        blogRepeater.DataSource = pgdArticles
        blogRepeater.DataBind()

    End Sub


    Private Function GetData() As PagedDataSource

        Dim pg As New PagedDataSource

        Dim conn As SqlConnection
        conn = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)

        Dim cmd As New SqlCommand("usp_MyProc", conn)

        Dim da As New SqlDataAdapter()
        da.SelectCommand = cmd
        Dim dt As New Data.DataTable()
        da.Fill(dt)
        pg.DataSource = dt.DefaultView

        Return pg

    End Function

它适用于使用中继器,但我想读取该数据并在我的表单上填充标签而不使用中继器。像下面这样......

我的数据在我的表中......

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        pgdArticles = GetData()

        If pgdArticles.ID = 1 Then
            lblName1 = pgdArticles.Name 'This will be value Mike
            lblValue1 = pgdArticles.Value 'This will be value 250
        End If

        If pgdArticles.ID = 2 Then
            lblName2 = pgdArticles.Name 'This will be value Ben
            lblValue2 = pgdArticles.Value 'This will be value 400
        End If

    End Sub

那么还有其他方法吗? C# 或 VB.NET 代码示例都可以。

【问题讨论】:

  • 您可以使用服务器内容控制并从后面的代码生成动态html来代替中继器。对于前 div.InnerHtml="
  • name
  • "
  • 不想使用这个,我只想填充页面各处的不同标签。
  • 你为什么要这个标签?
  • 这是当前构建 HTML 的方式。
  • 标签: c# asp.net vb.net datatable


    【解决方案1】:

    代码:

    Public Property dtArticles As DataTable
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    
        dtArticles = GetData()
    
        For I As Integer = 0 To dtArticles.Rows.Count - 1
            Dim row As DataRow = dtArticles.Rows(I)
            Dim label As Label = Page.FindControl(String.Format("Name{0}", I))
            label.Text = String.Format("{0} ({1})", row("Name"), row("Value"))
        Next
    
    End Sub
    
    Private Function GetData() As DataTable
    
        Dim conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)
        Dim cmd As New SqlCommand("usp_MyProc", conn)
    
        Dim da As New SqlDataAdapter()
        da.SelectCommand = cmd
    
        Dim dt As New Data.DataTable()
        da.Fill(dt)
    
        Return dt
    
    End Function
    

    HTML:

    <asp:Label ID="Name0" runat="server" />
    <asp:Label ID="Name1" runat="server" />
    <asp:Label ID="Name2" runat="server" />
    

    【讨论】:

    • 请编辑您之前的答案,不要再发一个
    • 这不是我想要的,我需要用正确的 ID 填充每个标签,并且它们不在 html 中的同一位置。
    • Panagiotis,对不起,我已经删除了第一个答案。 Etienne,我已更改代码,请重新审核。
    • 请查看您的代码部分,而不是跟随您在那里所做的事情或 GetData() 代码的外观。
    • 是的!没问题。 StackOverflow 上的第一点。新人,但有多年开发经验。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签