using Eval ("ZZZ")

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
 
G

Guest

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
 
R

Roland Dick

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
 
V

vMike

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top