【发布时间】: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