Passing value into DataBinder.Eval(Container.DataItem, "Property")

T

Tom Lee

Hi all,
I have the following problem and I cannot solve it. If
anyone can help me solve this problem.
I use the following code
<%#DataBinder.Eval(Container.DataItem, "Property")%>
to display Property from database. How do I dynamically
change the value "Property" to another one say "Name" to
display the Name from database or to any other variable
that I want.
Thanks.
 
L

lostinet

<%#DataBinder.Eval(Container.DataItem,
DataBinder.Eval(Container.DataItem,"PropertyName","{0}")
)%>
 
C

Chris Carter

Define a function and bind to that(assume your datasource is a DataView):

public string GetDynamicValue(object dataItem)
{
DataRowView drv = dataItem As DataRowView;
string fieldValue = "N/A";
string fieldToReturn = "";
if (somecondition)
{
fieldToReturn = "LastName";
}
else
{
fieldToReturn = "FirstName";
}
if (drv != null)
{
fieldValue = drv[fieldToReturn].ToString();
}
return fieldValue;
}

then in your script:

<%# GetDynamicValue ( Container.DataItem ) %>

if you're not sure what type your DataItem is, you can start with this:

public string GetDynamicValue ( object dataItem )
{
return dataItem.ToString();
}

-chris
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top