4

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

if the 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 does change but not displayed in repeater ( the old value is displayed).

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

other solution is by jQuery - to search the empty last TD.

but - my question :

is there any other server side solution ( ) ?

Royi Namir
  • 144,742
  • 138
  • 468
  • 792

3 Answers3

7

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 Method you can able to check by string that Empty or not than return string.

Jignesh Rajput
  • 3,538
  • 30
  • 50
  • silly me , how didnt i think about it.... - guess i was too attached to the datarowview class... – Royi Namir May 06 '12 at 11:56
  • have a look here : http://stackoverflow.com/questions/9820464/got-anyway-i-set-class-inside-repeater-datarow-find-by-data-id-when-do-postback. same things what you want – Jignesh Rajput May 06 '12 at 12:07
  • your solution is fine , but the link you gave doesnt answer my question since its another issue. i need to set a value and he isnt setting any value – Royi Namir May 06 '12 at 12:18
1

You could try to use the itemcreated event which occurs before the control is bound and not after the control is bound. Example in 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 an item in the Repeater control is data-bound but before it is rendered on the page.

Magnus Karlsson
  • 3,549
  • 3
  • 31
  • 57
1

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

Learning
  • 19,469
  • 39
  • 180
  • 373