2012-06-25 89 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