2013-05-10 20 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