2012-06-25 88 views
1

我很难理解中继器应该如何工作以及我应该如何使用它们。基本上,我有一个标签,一组图像按钮和一个中继器内的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. 我无法获得中继器数据源的Subject(我的数据库中的字段)出现的标签。

我真的很感激任何帮助。我无法掌握这些中继器控件。

+1

这不是ASP经典。 – BradBrening

回答

0

你想要做的是使用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> 
+0

这不适合我。作为新闻线和昏暗的新闻列线的昏暗新闻出现了错误。 我要做的是从空白页开始。没有什么。我的第一步是什么... – Lenigod

+0

我只提供了示例代码,您将不得不根据您的要求进行调整。我使用了一个News对象作为示例,它不会在您的上下文中定义。你将不得不使用你的数据源。 – ThePower

+0

显然这只是一个例子,但使用数据源并不能解决问题。尽管非常感谢你的努力。 – Lenigod