c# - 将整个 Container.DataItem 行传递给代码隐藏

标签 c# exception .net-3.5 itemtemplate datarowview

这个问题与 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/

相关文章:

c# - 引擎脚本和类结构

java - 如何识别哪个字段导致异常

c# - LINQ to SQL 中的内部连接不起作用

java - UnsatisfiedLinkError: java.library.path 中没有 j3dcore-ogl

c# - .net 3.5 中的协变

c# - 寻找带有 JSON 客户端库的 REST

c# - IHostedService 的多种实现

c# - FromArgb 与 FromScRgb

c# - 网站和 C# winforms 应用程序之间的通信

c# - 捕获多个自定义FaultException类型