Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetching values from the repeater control in ItemDataBound event

Tags:

asp.net

how to fetch values of repeater control in the ItemDataBound event i wrote the following asp code

<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>

and the ItemDataBound event in vb.net

 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

but i get empty value in content and url... please help

like image 313
Neha Raje Avatar asked Dec 13 '22 14:12

Neha Raje


1 Answers

First of all in markup use Text property of label, do not put databinder.Eval in

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

In item databound event first find out control in which template.

follow this example :

Markup:

<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>

Source Code :

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
like image 61
hemant kambli Avatar answered May 12 '23 07:05

hemant kambli