Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 700
Royi Namir Avatar asked Jan 17 '23 18:01

Royi Namir


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

like image 115
Jignesh Rajput Avatar answered Jan 29 '23 07:01

Jignesh Rajput


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.

like image 39
Magnus Karlsson Avatar answered Jan 29 '23 07:01

Magnus Karlsson