【问题标题】:Error Passing ListItemCollection via ObjectDataSource to Custom Adapter通过 ObjectDataSource 将 ListItemCollection 传递给自定义适配器时出错
【发布时间】:2012-11-15 19:03:59
【问题描述】:

我有一个 ObjectDataSource 声明如下:

<asp:ObjectDataSource ID="PaymentsDataSource"
                      runat="server"
                      DataObjectTypeName="Payment"
                      TypeName="PaymentAdapter"
                      SelectMethod="GetPayments">
    <SelectParameters>
        <asp:ControlParameter ControlID="StartDate"
                              PropertyName="Text"
                              Name="startDate"
                              Type="DateTime" />
        <asp:ControlParameter ControlID="EndDate"
                              PropertyName="Text"
                              Name="endDate"
                              Type="DateTime" />
        <asp:ControlParameter ControlID="LocationCodes"
                              PropertyName="Items"
                              Name="selectedLocationCodes"
                              Type="Object" />
    </SelectParameters>
</asp:ObjectDataSource>

控制问题是LocationCodes,声明如下:

<select id="LocationCodes"
        disabled="disabled"
        runat="server"
        class="chzn-container"
        multiple=""
        data-placeholder="Choose a Location Code(s)"
        style="width: 100%;"
        data-class="span10">
</select>

适配器上的服务器端Select方法定义如下:

[DataObjectMethod(DataObjectMethodType.Select, true)]
public List<Payment> GetPayments(
    DateTime startDate,
    DateTime endDate,
    object selectedLocationCodes)
{
}

现在,即使ListItemCollection 成功传递到Select 方法,Select 方法中的代码也可以正常处理,以下Application_Error 中仍会被动抛出错误。有没有办法忽略这个错误?

11/15/2012 13:37:49 168 (Machine=, App=34a846f1, Project=null, Dept=null, Thread=007, TraceLevel=1)
    Exception Source: mscorlib
    Exception Type: System.Runtime.Serialization.SerializationException
    Exception Message: Type 'System.Web.UI.WebControls.ListItemCollection' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

【问题讨论】:

    标签: c# asp.net serialization webforms objectdatasource


    【解决方案1】:

    好的,我解决这个问题的方法是通过添加 ViewStateMode="Disabled" 来关闭 ObjectDataSource 元素上的 ViewState

    【讨论】:

      【解决方案2】:

      问题是你不能序列化一个对象。如果你要改变 “object selectedLocationCodes” 到某种预定义的类应该没有问题。 为了将对象序列化,基本上需要将其分解为 XML,这意味着它需要知道每个字段是什么类型的数据。对象的描述不够具体,因为它可以是任何东西。

      【讨论】:

        猜你喜欢
        • 2021-03-28
        • 1970-01-01
        • 1970-01-01
        • 2015-10-10
        • 2017-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-24
        相关资源
        最近更新 更多