Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ASP.NET中继器中格式化数据_Asp.net_Repeater - Fatal编程技术网

在ASP.NET中继器中格式化数据

在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

我有一个使用ASP.NET转发器的ASP.NET web表单。该中继器的数据源是一个DataTable,其中包括一个名为“City”的列和一个名为“State”的列。在Repeater的ItemTemplate中,我想调用我编写的名为“FormatLocation”的自定义方法。该方法定义为:

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    
}