Change repeater string value?

im trying to change the value inside my repeater: (via the itemdatabound event)

if year is empty - set value blabla

my repeater:

  <ItemTemplate> <tr > <td > <%#Eval("year") %> </td> 

my c # code:

  void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { if (string.IsNullOrEmpty(((DataRowView)e.Item.DataItem)["year"].ToString())) { (((DataRowView)e.Item.DataItem)["year"]) = "blabla"; // ??????? } } 

it changes, but is not displayed in the repeater (the old value is displayed).

one solution is to add server control or literal (server runat) to itemTemplate - and " findControl " on the server - and change its value.

another solution - jQuery - to find the last empty TD.

but - my question is:

is there any other server side solution ()?

+4
source share
3 answers

you can try something like this:

Repeater in .aspx:

 <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <table> <tr> <td> <%# GetText(Container.DataItem) %></td> </tr> </table> </ItemTemplate> </asp:Repeater> 

.cs :

  protected static string GetText(object dataItem) { string year = Convert.ToString(DataBinder.Eval(dataItem, "year")); if (!string.IsNullOrEmpty(year)) { return year; } else { return "blahblah"; } } 

IN GetText A method that you can check for a string that is empty or not than the return string.

+6
source

HTML FILE

 <asp:Repeater ID="RPT_Bordereaux" runat="server"> <ItemTemplate> <table> <tr> <td> <%# GetValue(Container.DataItem) %></td> </tr> </table> </ItemTemplate> </asp:Repeater> 

.CS CODE

 protected void RPT_Bordereaux_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { } } protected static string GetValue(object dataItem) { string year = Convert.ToString(DataBinder.Eval(dataItem, "year")); if (!string.IsNullOrEmpty(year)) { return Convert.ToString(year); } else { return "blahbla"; } } 

This should work

+1
source

You can try to use an event created with the element that occurs before the control is bound, and not after the control is bound. Example in the first link:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.itemcreated.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater_events.aspx

ItemDataBound Occurs after the item in the Repeater control is bound to data, but before it is displayed on the page.

0
source

Source: https://habr.com/ru/post/1410953/


All Articles