【问题标题】:Problems with repeater controls中继器控制问题
【发布时间】:2012-06-26 01:57:42
【问题描述】:

我很难理解中继器应该如何工作以及应该如何使用它们。基本上,我在转发器中有一个标签、一组图像按钮和一个 div。当您单击按钮时,我想填充使用这些中继器创建的 div(因此中继器的每次迭代都有不同的 div)。

看起来很简单,但我什么都做不了。请帮帮我!

如果有帮助,这是我的一些代码。

(我的数据源)

<asp:sqldatasource runat="server" id="dtsSpecialNotes" connectionstring="connection string" providername="System.Data.SqlClient"> /asp:sqldatasource

(我的中继器)

<asp:Repeater id="rptSpecialNotes" runat="server">
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
    </ItemTemplate>
</asp:Repeater>

(后面有一些代码,我有一个方法来填充页面加载时调用的转发器)

rptSpecialNotes.DataSource = dtsSpecialNotes
rptSpecialNotes.DataBind()

Dim imbMotion As New ImageButton 
Dim imbAttachments As New ImageButton

imbMotion.ImageUrl = "images/controls/Exclaim.png" 
imbMotion.Width = "40"  
imbMotion.Height = "40" 
imbMotion.AlternateText = "Add Motion" 
imbMotion.ID = "imbMotion" & listSpecialNotesCounter
imbAttachments.ImageUrl = "images/controls/Documents.png"
imbAttachments.Width = "40"
imbAttachments.Height = "40"
imbAttachments.AlternateText = "Add Document"
imbAttachments.ID = "imbAttachments" & listSpecialNotesCounter

rptSpecialNotes.Controls.Add(lblTitle) 
rptSpecialNotes.Controls.Add(imbMotion)

以下是我遇到的问题:

  1. 我无法让中继器进行迭代。它只触发一次然后停止。
  2. 我无法显示带有转发器数据源主题(我的数据库中的一个字段)的标签。

我非常感谢任何帮助。我只是无法掌握这些中继器控件。

【问题讨论】:

  • 这不是 ASP 经典。

标签: asp.net vb.net


【解决方案1】:

您想要做的是使用ItemDataBound 事件而不是通过项目集合来填充您的转发器。这将为您分配给DataSource 的集合中的每个项目调用。

这是我在处理转发器数据绑定和 ItemDataBound 事件时所做的,我将在此示例中使用新闻项目列表:

' Bind my collection to the repeater
Private Sub LoadData()
    Dim newsList As List(Of News) = dao.GetNewsList()
    rptrNews.DataSource = newsList
    rptrNews.DataBind()
End Sub

' Every item in the data binded list will come through here, to get the item it will be the object e.Item.DataItem
Protected Sub rptrNews_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        ' Get the current item that is being data bound
        Dim news As News = DirectCast(e.Item.DataItem, News)

        ' Then get all the controls that are within the <ItemTemplate> or other sections and populate them with the correct data, in your case it would be an image
        Dim ltlHeadline As Literal = DirectCast(e.Item.FindControl("ltlHeadline"), Literal)
        Dim ltlContent As Literal = DirectCast(e.Item.FindControl("ltlContent"), Literal)

        ltlHeadline.Text = news.Headline
        ltlContent.Text = news.Teaser
    End If
End Sub

或者,您可以在标记代码中完成所有操作,只分配数据源并在后面的代码中调用数据绑定。为此,您可以按如下方式定义您的 ItemTemplate(再次使用新闻,例如):

<ItemTemplate>
  <tr>
    <td><%#Container.DataItem("Headline")%></td>
    <td><%#Container.DataItem("Teaser")%></td>
  </tr>
</ItemTemplate>

【讨论】:

  • 这对我不起作用。作为新闻行的昏暗新闻和昏暗的新闻列表行会出现错误。我要做的是从空白页开始。上面什么都没有。我的第一步是什么……
  • 我只提供了示例代码,您必须对其进行定制以满足您的要求。我以 News 对象为例,它不会在您的上下文中定义。您必须使用您的数据源。
  • 显然这只是一个例子,但使用数据源并不能解决问题。非常感谢您的尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
相关资源
最近更新 更多