在ASP.NET中继器中格式化数据 [英] Format Data in an ASP.NET Repeater

查看:75
本文介绍了在ASP.NET中继器中格式化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ASP.NET Repeater的ASP.NET Web表单.此转发器的数据源是一个DataTable,该数据表包括称为城市"的一列和称为状态"的一列.在Repeater的ItemTemplate中,我想调用一个我编写的自定义方法"FormatLocation".此方法定义为:

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;
}

我想在ItemTemplate中绑定数据时调用此方法,以便结果出现在UI中.有人可以告诉我该怎么做吗?谢谢!

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!

推荐答案

如果从数据库中获取它们,则可以这样做在中继器上

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

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

关于后面的代码

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    
}

如果它们不是来自数据库,而是来自列表,则对象oItem是数据本身.

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

这篇关于在ASP.NET中继器中格式化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆