1

Page_Loadにバインドされたデータソースがありますrepeater

ItemDataBoundのページに結果を書き込んでいますが、それがデータの最後の行である場合は、少し異なることを行う必要があります。

リピーターのItemDataBound内からPage_Loadのデータソースの行数にアクセスするにはどうすればよいですか?

私はもう試した:

Dim iCount As Integer
iCount = (reWorkTags.Items.Count - 1)
If e.Item.ItemIndex = iCount Then
    'do for the last row
Else
    'do for all other rows
End If

ただし、e.Item.ItemIndexとiCountは、すべての行で同じです。

助けてくれてありがとう。J。

4

4 に答える 4

4

ただし、e.Item.ItemIndex と iCount はすべての行で同じです。

これは、アイテムがまだ拘束力があるためです。Count は、バインド時に現在のアイテム インデックスの +1 になります。

repeaterが完全にバインドされた後にこれを行うのが最善だと思います。

したがって、以下を に追加できますPage_Load

rep.DataBind()

For each item as repeateritem in rep.items
   if item.ItemIndex = (rep.Items.Count-1)
       'do for the last row
   else
       'do for all other rows
   end if
Next

注:rep.DataBind()リピーターがバインドされた後にこれを実行する必要があることを示すために追加しました。

于 2011-12-09T12:45:36.687 に答える
1

セッションの使用を避けようとしていましたが、最終的にはセッションで動作するようになりました。

行数のセッションを作成したところ、ItemDataBound からアクセスできました。

Protected Sub reWorkTags_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles reWorkTags.ItemDataBound


    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

        Dim rowView As System.Data.DataRowView
        rowView = CType(e.Item.DataItem, System.Data.DataRowView)

        Dim link As New HyperLink
        link.Text = rowView("tag")
        link.NavigateUrl = rowView("tagLink")
        link.ToolTip = "View more " & rowView("tag") & " work samples"

        Dim comma As New LiteralControl
        comma.Text = ", "

        Dim workTags1 As PlaceHolder = CType(e.Item.FindControl("Linkholder"), PlaceHolder)

        If e.Item.ItemIndex = Session("iCount") Then
            workTags1.Controls.Add(link)
        Else
            workTags1.Controls.Add(link)
            workTags1.Controls.Add(comma)
        End If

    End If

End Sub
于 2011-12-09T13:11:05.930 に答える
1

これは古い質問ですが、最近まさにこの状況がありました。最後の項目を除くすべての項目のマークアップを書き出す必要がありました。

ユーザー コントロール クラスにプライベート メンバー変数を作成し、それをリピーターにバインドされているデータ ソースのカウント プロパティに設定し、そこから 1 を引きました。インデックスは 0 ベースであるため、インデックス値はカウントから 1 つずれています。

プライベート ロング itemCount { get; 設定; }

Page_Load または DataBind を呼び出す任意のメソッドで:

            //Get the count of items in the data source. Subtract 1 for 0 based index.
            itemCount = contacts.Count-1;

            this.repContacts.DataSource = contacts;
            this.repContacts.DataBind();

最後に、バインディング メソッドで

           //If the item index is not = to the item count of the datasource - 1

            if (e.Item.ItemIndex != itemCount)
                Do Something....
于 2015-03-28T21:45:37.333 に答える