在ASP.NET中继器中格式化数据
我有一个使用ASP.NET转发器的ASP.NET web表单。该中继器的数据源是一个DataTable,其中包括一个名为“City”的列和一个名为“State”的列。在Repeater的ItemTemplate中,我想调用我编写的名为“FormatLocation”的自定义方法。该方法定义为:在ASP.NET中继器中格式化数据,asp.net,repeater,Asp.net,Repeater,我有一个使用ASP.NET转发器的ASP.NET web表单。该中继器的数据源是一个DataTable,其中包括一个名为“City”的列和一个名为“State”的列。在Repeater的ItemTemplate中,我想调用我编写的名为“FormatLocation”的自定义方法。该方法定义为: protected string FormatLocation(string city, string state) { string location = city; if (state.Len
protected string FormatLocation(string city, string state)
{
string location = city;
if (state.Length > 0)
location += ", " + state.ToUpper();
return location;
}
我想在ItemTemplate中绑定数据时调用此方法,以便结果显示在UI中。有人能告诉我怎么做吗?谢谢 如果从数据库中获取它们,您可以这样做 在中继器上
<ItemTemplate>
<%#FormatLocation(Container.DataItem)%>
</ItemTemplate>
如果它们不是来自数据库,而是来自列表,则对象oItem就是数据本身
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
}