2017-03-28 95 views
0

所以我有兩個下拉列表在我的代碼中從SQL表中檢索數據。如果語句爲下拉列表

我找不到一個IF語句顯示錯誤消息,如果這兩個下拉列表都沒有被選中。我已經添加了一個列表項來告訴用戶選擇一個日期/時間。

我嘗試了一條if語句,但這不起作用。

如果聲明:

If DropDownList2.SelectedValue And DropDownList3.SelectedValue = Nothing Then 

     warninginfo.Visible = True 
    Else 
     Response.Redirect("ConfirmationBooking.aspx") 

    End If 

代碼:

<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource2" DataTextField="Time" DataValueField="Time" AppendDataBoundItems="True" width="162px" Height="31px"> 
    <asp:ListItem>-- Please Select a Time --</asp:ListItem> 
    </asp:DropDownList> 

    <asp:DropDownList ID="DropDownList2" runat="server" CssClass="auto-style2" DataSourceID="SqlDataSource1" DataTextField="Date" DataValueField="Date" AppendDataBoundItems="True" Width="162px" Height="30px"> 
    <asp:ListItem>-- Please Select a Date --</asp:ListItem> 
    </asp:DropDownList> 
+0

嘗試'DropDownList2.SelectedValue一無所有,DropDownList3.SelectedValue是Nothing' – LarsTech

+0

所以兩者都做有什麼選擇,也可以其中的一個字段不能選擇有哪些?我提到這一點,因爲如果'DropDownList2.SelectedValue是Nothing並且DropDownList3.SelectedValue是Nothing',那麼如果其中一個沒有選擇任何東西,它仍然會通過。更好的解決方案是'DropDownList2.SelectedValue是Nothing並且DropDownList3.SelectedValue是Nothing' ... – Codexer

回答

1

你如果說法是錯誤的。它應該是(將AND更改爲)

If DropDownList2.SelectedValue Is Nothing Or DropDownList3.SelectedValue Is Nothing Then 

另一種方法是使用RequiredFieldValidator來設置這兩個字段。

<asp:RequiredFieldValidator id="reqFavoriteColor" Text="(Required)" 
    InitialValue="none" ControlToValidate="DropDownList2" Runat="server" 
/>