0

再次抱歉,专家再次打扰。但是

我们有一个名为 orderdate 的数据库日期。

如果今天的日期 - orderdate 少于 2 天(或 48 小时),请禁用Cancel Order按钮,以便用户无法取消他或她的订单。

当我尝试运行以下代码时,我得到Input string not in the format

Orderdate 是日期时间类型。但是,我们希望以 MM/dd/yyyy 的格式显示日期。示例:2013 年 6 月 4 日,而不是 2013 年 6 月 4 日。

你能看看我的代码并告诉我我做错了什么吗?

 If dr1.Read() Then
  Dim odate As String = DateTime.Parse(dr1("orderDates").ToString()).ToShortDateString()

  Dim cancelBtn As New Button()
  Dim dates As String = DateTime.Parse(Now().ToString()).ToShortDateString()
   If (sdate - dates) <2 Then
       cancelBtn.Enabled = False
   Else
       cancelBtn.Enabled = True
   End If
 End If

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ' Create cancel training button
    Dim cancelBtn As New Button()
    cancelBtn.Style.Add("width", "105px")
    cancelBtn.Style.Add("padding", "5px")
    cancelBtn.Style.Add("margin", "5px")
    'cancelBtn.Enabled = False
    cancelBtn.Text = "Cancel training"
    If e.Row.RowIndex > "-1" Then
        ' Change tracking ID to link
        'Dim track As [String] = e.Row.Cells(4).Text
        'If track <> "&nbsp;" AndAlso track.Length > 0 Then
        '    Dim trackLink As New Literal()
        '    trackLink.Text = "<a style='color: blue;' href='" + track + "'/>Track</a>"
        '    e.Row.Cells(4).Controls.Add(trackLink)
        'End If

        ' Add buttons to column
        Dim oid As [String] = e.Row.Cells(0).Text
        Dim invoiceLink As New Literal()
        invoiceLink.Text = "<a style='color: blue;' href='Invoice.aspx?oid=" + oid + "'/>" + oid + "</a>"
        e.Row.Cells(0).Controls.Add(invoiceLink)
        e.Row.Cells(e.Row.Cells.Count - 1).Controls.Add(cancelBtn)

        ' Pass order id & row to on-click event
        'cancelBtn.Click += new EventHandler(this.cancelBtn_Click);
        'cancelBtn.CommandArgument = e.Row.RowIndex + "-" + oid
    End If
End Sub
4

3 回答 3

2

我不确定您为什么要将日期字段转换为字符串。我建议将它们保留为日期时间对象以供您比较。您始终可以在演示逻辑中操作日期的显示。

Subtract这是一些使用and保留为日期的工作代码TotalDays

Dim cancelBtn As New Button()
Dim odate As DateTime = DateTime.Parse(dr1("orderDates").ToString())
Dim dates As DateTime = DateTime.Now()

If dates.Subtract(odate).TotalDays >= 2 Then
    cancelBtn.Enabled = False
Else
    cancelBtn.Enabled = True
End If

您还可以将If语句合并为一行:

cancelBtn.Enabled = dates.Subtract(odate).TotalDays < 2
于 2013-06-05T04:03:57.290 回答
0
DateTime.Parse(Now().ToString()).ToShortDateString()

用。。。来代替

Today

否则,不要操纵字符串中的日期。

于 2013-06-05T03:56:42.037 回答
0

编辑:关于逻辑,您的数据库字段orderDates听起来像是指创建订单的日期。那个日期将永远是过去,所以我们会对Today - orderDates.

但是,根据您的评论,它似乎orderDates是指订单发货的日期。该日期必须在未来 2 天以上,用户才能取消订单,因此我们对orderDates - Today.

我看不到您在哪里运行订单日期逻辑GridView1_RowDataBound

Private Function MoreThanTwoDaysUntilShip() As Boolean
    'logic to open dr1
    If dr1.Read() Then
        Dim shipDate As Date = DateTime.Parse(dr1("orderDates").ToString())
        Dim today As Date = Date.Now

        Return shipDate.Subtract(today).TotalDays > 2
    End If

    Return False
End Function

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ' Create cancel training button
    Dim cancelBtn As New Button()
    cancelBtn.Style.Add("width", "105px")
    cancelBtn.Style.Add("padding", "5px")
    cancelBtn.Style.Add("margin", "5px")

    'add this
    cancelBtn.Enabled = MoreThanTwoDaysUntilShip()


    'etc
End Sub
于 2013-06-05T06:57:41.773 回答