如何在asp.net中禁用多按钮

时间:2015-03-20 07:39:49

标签: asp.net vb.net

    Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged



    Dim db As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\jonhny\Desktop\FinalProject\Backup\Backup\Backup\FinalProject\Database.accdb'")
    If db.State = ConnectionState.Open Then
        db.Close()
    End If

    db.Open()

    Dim reservationDate As String = Calendar1.SelectedDate
    Dim reserved As String


    Dim table As String


    Dim booking As String = "SELECT BookingDate, NumberOfTable FROM CustomerReservation WHERE BookingDate='" & reservationDate & "'"
    Dim sqlcommand1 As New OleDbCommand(booking, db)

    Dim dr1 As OleDbDataReader

    dr1 = sqlcommand1.ExecuteReader


    If dr1.HasRows Then
        dr1.Read()

        reserved = dr1.Item("BookingDate")
        Table = dr1.Item("NumberOfTable")

    End If
    dr1.Close()


    If reservationDate < DateTime.Now.AddDays(-1) Then
        lblShowMessage.Text = "*You can't select previous date!"
        lblShowMessage.ForeColor = Drawing.Color.Red
        lblShowDate.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = False

    ElseIf reservationDate < DateTime.Now.AddDays(7) Then
        lblShowMessage.Text = "*You have to reserve 7 days earlier!"
        lblShowMessage.ForeColor = Drawing.Color.Red
        lblShowDate.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = False

    ElseIf reservationDate = reserved Then

        lblShowMessage.Text = "*This day not Available!"
        lblShowMessage.ForeColor = Drawing.Color.Red
        lblShowDate.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = False

        Button1.Visible = True
        Button2.Visible = True
        Button3.Visible = True
        Button4.Visible = True
        Button5.Visible = True
        Button6.Visible = True
        Button7.Visible = True
        Button8.Visible = True
        Button9.Visible = True
        Button11.Visible = True
        Button12.Visible = True
        Button13.Visible = True
        Button14.Visible = True
        Button15.Visible = True
        Button16.Visible = True
        Button17.Visible = True
        Button18.Visible = True



        If Button1.Text = table Then
            Button1.Visible = False
        End If

        If Button2.Text = table Then
            Button2.Visible = False
        End If


        If Button3.Text = table Then
            Button3.Visible = False
        End If

        If Button7.Text = table Then
            Button7.Visible = False
        End If
        If Button11.Text = table Then
            Button11.Visible = False
        End If
    Else
        lblShowMessage.Text = "Available to Reserve"
        lblShowMessage.ForeColor = Drawing.Color.Green
        lblShowDate.Text = Calendar1.SelectedDate
        BookingdateTextBox.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = True
        Button1.Visible = True
        Button2.Visible = True
        Button3.Visible = True
        Button4.Visible = True
        Button5.Visible = True
        Button6.Visible = True
        Button7.Visible = True
        Button8.Visible = True
        Button9.Visible = True
        Button11.Visible = True
        Button12.Visible = True
    End If
    db.Close()
End Sub

它只能禁用1个按钮,如何同时禁用2个以上的按钮?

1 个答案:

答案 0 :(得分:0)

我建议您更改逻辑以包含while循环:

    dr1 = sqlcommand1.ExecuteReader

Dim count As Integer = dr1.FieldCount
        While reader.Read()
            For i As Integer = 0 To count - 1
                reserved = dr1.Item("BookingDate")
                Table = dr1.Item("NumberOfTable")

                If reservationDate < DateTime.Now.AddDays(-1) Then
        lblShowMessage.Text = "*You can't select previous date!"
        lblShowMessage.ForeColor = Drawing.Color.Red
        lblShowDate.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = False

    ElseIf reservationDate < DateTime.Now.AddDays(7) Then
        lblShowMessage.Text = "*You have to reserve 7 days earlier!"
        lblShowMessage.ForeColor = Drawing.Color.Red
        lblShowDate.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = False

    ElseIf reservationDate = reserved Then

        lblShowMessage.Text = "*This day not Available!"
        lblShowMessage.ForeColor = Drawing.Color.Red
        lblShowDate.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = False

        Button1.Visible = True
        Button2.Visible = True
        Button3.Visible = True
        Button4.Visible = True
        Button5.Visible = True
        Button6.Visible = True
        Button7.Visible = True
        Button8.Visible = True
        Button9.Visible = True
        Button11.Visible = True
        Button12.Visible = True
        Button13.Visible = True
        Button14.Visible = True
        Button15.Visible = True
        Button16.Visible = True
        Button17.Visible = True
        Button18.Visible = True



        If Button1.Text = table Then
            Button1.Visible = False
        End If

        If Button2.Text = table Then
            Button2.Visible = False
        End If


        If Button3.Text = table Then
            Button3.Visible = False
        End If

        If Button7.Text = table Then
            Button7.Visible = False
        End If
        If Button11.Text = table Then
            Button11.Visible = False
        End If
    Else
        lblShowMessage.Text = "Available to Reserve"
        lblShowMessage.ForeColor = Drawing.Color.Green
        lblShowDate.Text = Calendar1.SelectedDate
        BookingdateTextBox.Text = Calendar1.SelectedDate
        btnPToPayment.Visible = True
        Button1.Visible = True
        Button2.Visible = True
        Button3.Visible = True
        Button4.Visible = True
        Button5.Visible = True
        Button6.Visible = True
        Button7.Visible = True
        Button8.Visible = True
        Button9.Visible = True
        Button11.Visible = True
        Button12.Visible = True
    End If
            Next
         End While

dr1.Close()

这样做会对数据读取器中的每一行进行检查以设置保留和表信息,然后允许您针对每一行而不是针对1行运行逻辑,这基本上就是你的行目前正在做。

其他建议

为您的按钮提供有意义的名称,例如:ReserveTableOne,ReserveTableTwo而不仅仅是button1和button2 - 稍后再次访问时会有所帮助。

If reservationDate < DateTime.Now.AddDays(-1)更改为If reservationDate <= DateTime.Now.AddDays(-1),否则只有当它低于昨天而不是小于或等于昨天时才会这样做 - 这就是您所需要的。

ElseIf reservationDate < DateTime.Now.AddDays(7)更改为ElseIf reservationDate > DateTime.Now.AddDays(7)我认为如果您只允许人们提前一周预订,我认为应该是正确的,否则您将无法保留超过7天的表格从现在开始。