using Eval ("ZZZ")

  • Thread starter Thread starter Rick Mavrovik
  • Start date Start date
R

Rick Mavrovik

Hi,
I am using repeater bound to a dataset.
Within the repeater I have got ItemTemplate in which I am displaying data by
using
Eval("DataFieldName").

Does anyone know how can I perform any calculation in ItemTemplate on the
bound data..For instance. I need to display an image based on some rule like
if Eval("ThisField") is greater than Eval("ThatField").

Any clue?

-Rick
 
Howdy,

Two ways:
1. Bound expressions

usually this gets messy for complex expressions

<ItemTemplate>
<asp:Label runat="server" ID="lab"
Text='<%# (int) Eval("DataField") > 0 ? "Greater" : "Less or Equal" %>'/>
</ItemTemplate>

2. Handling ItemDataBound event
protected void repeater_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;

if (item.ItemType == ListItemType.Item ||
item.ItemType == ListItemType.AlternatingItem)
{
Label label = (Label)item.FindControl("lab");

DataRow row = ((DataRowView) item.DataItem).Row;

int value = (int) row["DataField"];

label.Text = value > 0 ? "Greater" : "Less or Equal";
}
}

hope this helps
 
Hi,

in addition to the solutions Milosz gave you, here's a third way: call a
code-behind method on the bound data.

<ItemTemplate>
<asp:Label runat="server" ID="lab"
Text='<%# MyMethod(Container.DataItem) %>'/>
</ItemTemplate>

and in the code behind:

protected string MyMethod (object DataItem)
{
DataRowView dr = DataItem as DataRowView;
if (dr == null)
return "";
return dr["Column1"].ToString() + dr["Column2"].ToString();
}

....or whatever you need to do there. You can also pass additional
parameters.

Roland
 
Rick Mavrovik said:
Hi,
I am using repeater bound to a dataset.
Within the repeater I have got ItemTemplate in which I am displaying data
by using
Eval("DataFieldName").

Does anyone know how can I perform any calculation in ItemTemplate on the
bound data..For instance. I need to display an image based on some rule
like if Eval("ThisField") is greater than Eval("ThatField").

Any clue?

-Rick
One additional solution -- add a column to your data which does the
calculation and then use that field name.

mike
 
Back
Top