从ItemDataBound事件中的转发器控件中获取值

时间:2011-03-21 05:22:11

标签: asp.net

如何在ItemDataBound事件中获取转发器控件的值 我写了以下asp代码

<asp:Repeater ID="repeaterItems" runat="server" OnItemDataBound="rptData_ItemDataBound"  >
        <itemtemplate>
                        <tr>
                            <td  style="font-family:Tahoma; font-size:small"><asp:Label id="content" runat="server">&nbsp;&nbsp;<%#DataBinder.Eval(Container.DataItem, "cont")%></asp:Label></td>

                        </tr>

                        <tr><td  style="font-family:Verdana; font-size:small"><asp:Label id="url" runat="server"><a style="color:Green" href="<%#DataBinder.Eval(Container.DataItem, "url")%>" target="_blank"><%#DataBinder.Eval(Container.DataItem, "url")%></a></asp:Label></td></tr>
                    <tr><td><asp:LinkButton runat="server" ID="glossary" >Glossary</asp:LinkButton></td></tr>
                    <tr><td><asp:HiddenField runat="server" ID="id1"/><%#DataBinder.Eval(Container.DataItem, "id")%></td></tr>
                    <tr><td></td></tr><tr><td></td></tr><tr><td></td></tr>
                    </itemtemplate>
        </asp:Repeater>

和vb.net中的ItemDataBound事件

 Protected Sub rptData_ItemDataBound(ByVal source As Object, ByVal e As RepeaterItemEventArgs)

        Dim content As String = ""
        Dim url As String = ""
        Dim lbl1 As Label = TryCast(e.Item.FindControl("content"), Label)
        Dim lbl2 As Label = TryCast(e.Item.FindControl("url"), Label)
        content = lbl1.Text
        url = lbl2.Text
        Dim btn As LinkButton = CType(e.Item.FindControl("glossary"), LinkButton)

        btn.PostBackUrl = "glossary.aspx?cont=" + content + "&url=" + url
end sub

但我在内容和网址中得到空值... 请帮忙

1 个答案:

答案 0 :(得分:12)

首先在标记中使用label的Text属性,不要将databinder.Eval放在

<asp:Label ..> do not put content here </asp:Label>

在项目数据绑定事件中,首先找出控件在哪个模板中。

按照这个例子:

标记:

<asp:Repeater ID="Repeater1" runat="server" 
                    onitemdatabound="Repeater1_ItemDataBound">                
                <HeaderTemplate>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("content") %>'></asp:Label>
                    <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
                </ItemTemplate>
                <AlternatingItemTemplate></AlternatingItemTemplate>
                <SeparatorTemplate></SeparatorTemplate>
            </asp:Repeater>

源代码:

C#:

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item)
        {
            Label lbl = (Label)e.Item.FindControl("Label1");
            LinkButton link = (LinkButton)e.Item.FindControl("LinkButton1");
            link.PostBackUrl = "abcd/" + lbl.Text;
        }
    }

VB.Net:

Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    If e.Item.ItemType = ListItemType.Item Then
        Dim lbl As Label = DirectCast(e.Item.FindControl("Label1"), Label)
        Dim link As LinkButton = DirectCast(e.Item.FindControl("LinkButton1"), LinkButton)
        link.PostBackUrl = "abcd/" + lbl.Text
    End If
End Sub