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

  • Thread starter Thread starter Tom Lee
  • Start date Start date
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.
 
<%#DataBinder.Eval(Container.DataItem,
DataBinder.Eval(Container.DataItem,"PropertyName","{0}")
)%>
 
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
 
Back
Top