2013-05-10 56 views
0

我一直在vb.net中使用中繼器,並且對它們沒有任何問題,但是我正在處理一些c#並且無法完成轉換(以及在線轉換jobby似乎沒有工作)從VB.net在C#中使用中繼器示例

我有我的中繼器設置出像:

<asp:Repeater runat="server" ID="rptItems"> 
       <ItemTemplate> 
       <div class="span12 grey-box"> 
          <div class="hero-block3"> 
           <div class="row show-grid"> 
            <div class="span9"> 
             <div class="hero-content-3"> 
              <h2><asp:Literal ID="ltrName" runat="server"></asp:Literal></h2> 
              <p><asp:Literal ID="ltrDescription" runat="server"></asp:Literal></p> 
             </div> 
            </div> 
            <div class="span3"> 
             <div class="tour-btn"> 
              <small>How Many?<br /></small> 
              <asp:TextBox runat="server" ID="tbox" Width="40"></asp:TextBox> 
             </div> 
            </div> 
           </div> 
          </div> 
         </div> 
       </ItemTemplate> 
      </asp:Repeater> 

而且繼承人如何我通常填充它在vb.net

Private Sub rptItems_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptClientList.ItemDataBound 
     Dim nRow As DataRowView = Nothing 

     Select Case e.Item.ItemType 
      Case ListItemType.Item, ListItemType.AlternatingItem 

       With e.Item 
        nRow = DirectCast(e.Item.DataItem, DataRowView) 
        With DirectCast(.FindControl("ltrText"), Literal) 
         .Text = nRow("Description") 
        End With 

        With DirectCast(.FindControl("ltrTitle"), Literal) 
         .Text = nRow("Name") 
        End With 

       End With 

     End Select 
    End Sub 
+0

你爲什麼不直接使用數據綁定?這是有原因的嗎? – 2013-05-10 15:03:54

+0

是的,我需要做一個if語句內的綁定循環基本上如果boolean = true,隱藏特定元素 – TMB87 2013-05-10 15:04:27

回答

2

你可以修改你的Repeater aspx指定事件處理程序:

<asp:Repeater runat="server" ID="rptItems" 
       OnItemDataBound="rptItems_ItemDataBound"> 
    ... 

然後在C#中的事件處理程序將

protected void rptItems_ItemDataBound(object sender, 
             System.Web.UI.WebControls.RepeaterItemEventArgs e) 
{ 
    DataRowView nRow = null; 

    switch (e.Item.ItemType) 
    { 
     case ListItemType.Item: 
     case ListItemType.AlternatingItem: 
      nRow = (DataRowView) e.Item.DataItem; 
      ((Literal)e.Item.FindControl("ltrText")).Text = nRow["Description"].ToString(); 
      ((Literal)e.Item.FindControl("ltrTitle")).Text = nRow["Name"].ToString(); 
     break; 
    } 
} 
+0

嘿,謝謝你。唯一的問題似乎是它不喜歡nRow(「無論」) - 「錯誤14'nRow'是一個'變量',但像'方法'一樣使用」 – TMB87 2013-05-10 15:13:43

+2

更改括號()for bracker [] – Stephan 2013-05-10 15:19:07

+0

My錯誤......剛剛糾正! – 2013-05-10 15:55:33