0

申し訳ありませんが、専門家の皆様、お手数をおかけして申し訳ありません。

orderdate というデータベース日付があります。

今日の日付 - 注文日が 2 日 (または 48 時間) 未満の場合は、Cancel Orderボタンを無効にして、ユーザーが注文をキャンセルできないようにします。

次のコードを実行しようとすると、Input string not in the format

Orderdate は datetime 型です。ただし、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ステートメントを 1 行に統合することもできます。

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

編集: ロジックに関しては、DB フィールド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 に答える