0

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

Shai Cohen
  • 6,074
  • 4
  • 31
  • 54
thegunner
  • 6,883
  • 30
  • 94
  • 143

3 Answers3

0

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
jason
  • 3,821
  • 10
  • 63
  • 120
  • Just a small pointer, if you are doing a `return` inside of an `if` statement, you don't need the `else` statement. – Shai Cohen Mar 28 '13 at 18:11
  • It'd be cleaner to just see it `e.Item.Itemtype` is either `Item` or `AlternatingItem` and then you don't need an "else" statement. – MikeSmithDev Mar 28 '13 at 18:14
0

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

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
0

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`

    }
}
Praveen Nambiar
  • 4,852
  • 1
  • 22
  • 31