Format Data in an ASP.NET Repeater

一世执手 提交于 2019-12-11 16:40:08

问题


I have an ASP.NET web form that uses an ASP.NET Repeater. The data source for this repeater is a DataTable that includes a column called "City", and one called "State". In the ItemTemplate of the Repeater, I want to call a custom method I've written called "FormatLocation". This method is defined as:

protected string FormatLocation(string city, string state)
{
  string location = city;
  if (state.Length > 0)
    location += ", " + state.ToUpper();
  return location;
}

I want to call this method when the data is bound in the ItemTemplate so that the result appears in the UI. Can someone tell me how to do this? Thanks!


回答1:


You can do this way if get them from Database On the Repeater

<ItemTemplate>
    <%#FormatLocation(Container.DataItem)%>
</ItemTemplate>

On code behind

protected string FormatLocation(object oItem)
{
    string city = DataBinder.Eval(oItem, "city").ToString();
    string state = DataBinder.Eval(oItem, "state").ToString();

     string location = city;
      if (state.Length > 0)
        location += ", " + state.ToUpper();
      return location    
}

If they are not from database, but from a list, the object oItem is the data himself.



来源:https://stackoverflow.com/questions/3937425/format-data-in-an-asp-net-repeater

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!