转发器数据绑定,具有某些数据的操作

时间:2011-08-03 08:49:25

标签: c# asp.net

下面是我的转发器代码,一切正常,但我遇到了问题:

<span class="txsources">Sources: </span>之后的最后,我想把转发器中的链接放入,在数据源后面的代码中有另一个包含链接的字段,但是这个fin这个字段,源是写的和分开的使用;,如:link1;link2;link3....

所以我被告知在代码后面编写一个函数来完成这项工作......在转发器中它的onitemdatabound,我该怎么做,以及在该函数中应该写什么?能够在;上拆分它们并将它们作为链接。

 <asp:Repeater runat="server" ID="rptArticleContent">
<ItemTemplate>
<tr>
<td width="365" valign="top" align="left" class="bodyContent" bgcolor="#FFFFFF">
<div>
<h2 class="h2">
<asp:Label runat="server" ID="dsds"> <%#Eval("Title") %></asp:Label>
</h2>
<div class="article-body">
<div class="Article-image">
<%#Eval("Image") %>
</div>
<%#Eval("Description") %>
</div>
<div class="horizontal_dotted_line1" align="right">
<a href="#top">>> Back to top</a></div>
<span class="txsources">Sources: </span><a href="#" target="_blank">Link 1</a> 
| <a href="#" target="_blank">Link 2</a> | <a href="#" target="_blank">Link 3</a>
| <a href="#" target="_blank">Link 4</a> | <a href="#" target="_blank">Link 5</a>
| <a href="#" target="_blank">Link 6</a></div>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

1 个答案:

答案 0 :(得分:2)

我会在用于包含链接的<asp:Literal />之后创建一个<span class="txsources">Sources: </span>控件。

在您的代码中,在ItemDataBound中,您可以执行以下操作:

Protected Sub rptArticleContent_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptArticleContent.ItemDataBound

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

            Dim Links As String = e.Item.DataItem("Links")
            Dim LinksStr As String() = Links.Split(";")

            Dim Ltl As Literal = e.Item.FindControl("Ltl")
            Dim Inc As Integer = 1
            For Each item As String In LinksStr
                Ltl.Text += String.Format("<a href='{0}'>Link {1}</a>", item, Inc)
                Inc += 1
            Next

    End Select

End Sub

C#等效代码:

protected void rptArticleContent_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    switch (e.Item.ItemType) {
        case ListItemType.Item:
        case ListItemType.AlternatingItem:

            string Links = e.Item.DataItem("Links");
            string[] LinksStr = Links.Split(";");

            Literal Ltl = e.Item.FindControl("Ltl");
            int Inc = 1;
            foreach (string item in LinksStr) {
                Ltl.Text += string.Format("<a href='{0}'>Link {1}</a>", item, Inc);
                Inc += 1;
            }


            break;
    }

}