Asp.net 中继器控制的问题

Asp.net 中继器控制的问题,asp.net,vb.net,Asp.net,Vb.net,我很难理解中继器应该如何工作以及我应该如何使用它们。基本上,我有一个标签、一组图像按钮和一个中继器内的div。当您单击按钮时,我想填充使用这些中继器创建的div(因此,中继器的每个迭代都有一个不同的div) 看起来很简单,但我什么都做不到。请帮帮我 这是我的一些代码,如果有帮助的话 (我的数据源) 以下是我遇到的问题: 我无法让中继器迭代。它只发射一次,然后停止 我无法获得显示转发器数据源主题(数据库中的字段)的标签 我真的很感谢你的帮助。我无法掌握这些中继器控件。您要做的是使用ItemData

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

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

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

(我的数据源)

以下是我遇到的问题:

  • 我无法让中继器迭代。它只发射一次,然后停止
  • 我无法获得显示转发器数据源主题(数据库中的字段)的标签

  • 我真的很感谢你的帮助。我无法掌握这些中继器控件。

    您要做的是使用
    ItemDataBound
    事件填充中继器,而不是遍历项目集合。将为分配给
    数据源的集合中的每个项调用

    这就是我在使用repeater数据绑定和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
    
    “将我的收藏绑定到中继器
    专用子加载数据()
    Dim newsList As List(Of News)=dao.GetNewsList()
    rptrNews.DataSource=新闻列表
    rptrNews.DataBind()
    端接头
    '数据绑定列表中的每个项都将通过此处,要获取该项,它将是对象e.item.DataItem
    受保护的子rptrNews_ItemDataBound(发送方作为对象,e作为RepeaterItemEventArgs)
    如果e.Item.ItemType=ListItemType.Item或LSE e.Item.ItemType=ListItemType.AlternatingItem,则
    '获取正在进行数据绑定的当前项
    Dim news As news=DirectCast(e.Item.DataItem,news)
    '然后获取或其他部分中的所有控件,并用正确的数据填充它们,在您的情况下,它将是一个图像
    Dim ltlHeadline As Literal=DirectCast(e.Item.FindControl(“ltlHeadline”),Literal)
    Dim ltlContent As Literal=DirectCast(例如,Item.FindControl(“ltlContent”),Literal)
    ltlHeadline.Text=news.Headline
    ltlContent.Text=news.trister
    如果结束
    端接头
    
    或者,您可以在标记代码中完成这一切,并且只在代码隐藏中分配数据源和调用数据绑定。要实现这一点,您可以按如下方式定义ItemTemplate(同样使用新闻,例如):

    
    
    这不是ASP经典版。这对我不起作用。作为新闻行的暗淡新闻和暗淡新闻列表行会出现错误。我要做的是从空白页开始。上面什么也没有。我的第一步是什么…我只提供了示例代码,您必须对其进行定制以满足您的需求。我使用了一个News对象作为示例,它不会在您的上下文中定义。您必须使用数据源。显然,这只是一个示例,但使用数据源并不能解决问题。非常感谢您的尝试。
    <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)
    
    ' 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>
      <tr>
        <td><%#Container.DataItem("Headline")%></td>
        <td><%#Container.DataItem("Teaser")%></td>
      </tr>
    </ItemTemplate>