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. リピーター データ ソースの件名 (データベース内のフィールド) を含むラベルが表示されません。

どんな助けでも本当に感謝しています。私はこれらのリピーターコントロールを理解できません。

4

1 に答える 1

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>
于 2012-06-25T15:36:38.727 に答える