Search code examples
c#asp.netwebformsrepeateritemdatabound

Change repeater row value?


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 ( ) ?


Solution

  • 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.