How can I modify a value before it is shown in a databound Repeater?
-
04-03-2021 - |
Question
I am databinding a repeater some text and a datetime(Respond By).
<asp:Repeater ID="rptList" OnItemDataBound="repeaterDatabound" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<b>Respond By</b>
</td>
<td>
<%#Eval("RespondBy")%>
</td>
</tr>
Here I want to change the datatime before I display it on the screen, I want to do a rowdatabound
similar to the one below. How can I do that for a repeater. Or is there anyother way of adding value(3Hrs) to Respond by dateTime before displaying it to users.
protected void repeaterDatabound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime.TryParse(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "RespondBy")), out Respond);
}
}
I cant add 3hrs in the client side, the no of hours I have to add is different for each users,
Solution
You can either do following in aspx file.
((DateTime)Eval("RespondBy")).AddHours(3)
Or add a label/literal in aspx instead of <%#Eval("RespondBy")%>
and use it in ItemDataBound
OTHER TIPS
You could either change your <%#Eval("RespondBy)%>
to some server control, a <asp:Literal>
for example, then update the value of this in your ItemDataBound
method.
Or you could use some inline code
<%#((DateTime) Eval("RespondBy")).AddHours(3).ToString("dd MMMM yyyy")%>
Or you could produce a public function which you can then reuse in the future.
public static string RespondByDate(DateTime theDate){
return theDate.AddHours(3).ToString("dd MMMM yyyy");
}