asp.net repeater – get value of row item _DataBound

how can I get the value of each cell that I want in a repeater _DataBound sub? – this is so I can change the value a little and apply the change to a literal

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Let’s say this is your repeater (since you didn’t include any code):

<asp:Repeater ID="_r" runat="server">
    <ItemTemplate>
        <asp:Literal ID="_lit" Text='<%# Eval("yourItemName")%>' runat="server"></asp:Literal>
        <br /><br />
    </ItemTemplate>
</asp:Repeater>

and you make an ItemDataBound event because you want to add “meow” to the end of every literal (which otherwise will just show the value of yourItemName from the datasource):

protected void Page_Load(object sender, EventArgs e)
{
    _r.DataSource = table;
    _r.ItemDataBound += new RepeaterItemEventHandler(RItemDataBound);
    _r.DataBind();
} 

protected void RItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal lit = (Literal)e.Item.FindControl("_lit");
        lit.Text += "meow";
    }
}

Reading: Repeater.ItemDataBound Event

Solution 2

You can get the literal in that row but casting the event arg:

Protected Sub repeater_dataBind(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles myRepeater.ItemDataBound
    If (e.Item.ItemType <> ListItemType.Item And e.Item.ItemType <> ListItemType.AlternatingItem) Then
            Return
    Else
       Dim myLiteral As New Literal
       myLiteral = CType(e.Item.FindControl("IDofLiteral"), Literal)
    End If

end Sub

Solution 3

You can use FindControl to get the values of a cell inside a repeater.

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Literal lbl = (Literal)e.Item.FindControl("ltrl");  
        lbl.Text = // will give you the value for each `ItemIndex`

    }
}

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply