【问题标题】:Cannot pass null value from asp:ObjectDataSource无法从 asp:ObjectDataSource 传递空值
【发布时间】:2015-09-16 16:34:43
【问题描述】:

下面给出的是我正在尝试从 texbox 传递日期时间值。它可以很好地传递所有其他值,但它将 null 值传递为 {1/1/0001 12:00:00 AM}。我希望它传递 null值。请帮忙?

<asp:ObjectDataSource ID="Schedules" runat="server" SelectMethod="GetScheduleByDate"
        TypeName="WebUI.Code" OldValuesParameterFormatString="original_{0}">
        <SelectParameters>
            <asp:ControlParameter ConvertEmptyStringToNull="true" ControlID="TextBoxSchedules" Name="ScheduleDate" PropertyName="Text" Type="DateTime"/>
        </SelectParameters>
    </asp:ObjectDataSource>

【问题讨论】:

  • DateTime 是一个结构体,在这个意义上不能为空,就像 int 或 long 一样。也许Nullable&lt;DateTime&gt; 会起作用。
  • 另一个选项是DateTime? myDatetime
  • 谢谢!我知道这个概念,但我并没有做所有正确的事情。现在我又试了一次,它起作用了!谢谢大家!
  • @rene 您应该将此作为答案发布。

标签: c# asp.net objectdatasource


【解决方案1】:

我通过以下代码解决了同样的问题:

<asp:ObjectDataSource ID="MyDataSource" runat="server" SelectMethod="GetSomething" TypeName="MyNamespace.MyAdapterType">
    <SelectParameters>
        <asp:Parameter Name="id" Type="Object" ConvertEmptyStringToNull="True" />
        <asp:Parameter Name="enabled" Type="Object" ConvertEmptyStringToNull="True" />
    </SelectParameters>
</asp:ObjectDataSource>

方法的签名

public class MyAdapterType
{
    public IEnumerable<CollectorPlugin> GetCollectorPlugin(Guid? id = null, bool? enabled = null)
    {
        //in debug mode i have here 2 null objects
        throw new NotImplementedException();
    }
}

【讨论】:

    猜你喜欢
    • 2013-08-08
    • 2020-09-02
    • 2017-11-16
    • 1970-01-01
    • 2020-06-02
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多