这个问题与 ASP.Net 3.5 和 C# 有关
我正在从一个包含很多列的数据库中构建一个 RSS Feed。
我需要对特定节点中的数据进行格式化,并且以内联方式进行操作会非常困惑。
我知道我可以将所有参数单独传递给一个子例程
<%# formatAddress(Container.DataItem["propertyName"],
Container.DataItem["propertyNumber"], ... ) %>
由于最多将有 20 个这样的列,我宁愿传递整行。
<%# formatAddress(Container.DataItem) %>
这将是理想的,然后我可以在后面的代码中挑选出我想要的列:
protected string FormattedAddress(object row)
{
DataRowView data = (DataRowView)row;
StringBuilder Address = new StringBuilder();
string Postcode = data["propertyPostcode"];
...
return Address.ToString();
}
我收到错误无法将类型为“System.Data.Common.DataRecordInternal”的对象转换为类型“System.Data.DataRowView”。
以前,我使用的是 protected 字符串 FormattedAddress(DataRowView row),但这也不起作用。
有什么线索吗?
最佳答案
最终找到了几个例子,让我意识到我应该转换为 DbDataRecord。
我仍在传递 <%# formattedAddress(Container.DataItem) %> 但我的函数现在如下所示:
protected string FormattedAddress(object dataItem)
{
DbDataRecord data = (DbDataRecord)dataItem;
string Postcode = data.GetValue(
data.GetOrdinal("propertyPostcode")).ToString();
...
return NicelyFormattedAddress;
}
这是处理数据的最佳方式吗?
关于c# - 将整个 Container.DataItem 行传递给代码隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12054130/