Groups | Blog | Home
all groups > asp.net building controls > january 2008 >

asp.net building controls : ItemDataBound event for Repeater



SushiSean
1/8/2008 2:19:03 PM
Hello. I have Repeater and DataSource some table and I need change
row in ItemDataBound event.

So code looks like this
<asp:Repeater ID="Repeater1" runat="server"
OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "id")%>
</ItemTemplate>
</asp:Repeater>

protected void Repeater1_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView row = e.Item.DataItem as DataRowView;
string id = row["id"].ToString(); //get value here
((DataRowView)e.Item.DataItem)["id"] = 5; //try to change it

}
}

The problem it doesn't have effect. In repeater I see the same id.
Riki
1/11/2008 6:44:42 PM
When you change the dataitem in ItemDataBound, it has already been added to
the repeater.
Therefore, when you change it there, it's too late already.

Do you want to change the result in the Repeater, or do you want to change
the data?
In the first case, use e.Item.Text to change the contents of the item that
has been bound:
e.Item.Text = 5;

--

Riki

[quoted text, click to view]

AddThis Social Bookmark Button