asp.netrepeateritemdatabound

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


Solution

  • 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