Drop Down Validation

Posted on 2006-05-03
Last Modified: 2012-05-05
hi guys,
i am making a simple form for my job and it has two drop down menus and a submit button,
the user has to either not select anything from the drop down and click submit to get the results ( * ) from a database, or if he/she select a value for one drop down, he/she has to select the other drop down also for it to work, its a all or none scenario.
here is the code that i have now,,,
anyone can help with the validation code on it?
<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Width="123px">
            <asp:ListItem Selected="True">*</asp:ListItem>
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" Width="123px">
            <asp:ListItem Selected="True">*</asp:ListItem>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br />
Question by:djsoltan
    LVL 14

    Expert Comment

    Remove the Autopostback from dropdown lists'. They are not needed here (I think)

    then in Button1_Click event:

    If DropDownList1.SelectedValue="*" and DropDownList2.SelectedValue<>"*" OR
    DropDownList1.SelectedValue<>"*" and DropDownList2.SelectedValue="*" Then
       'report error
       'query database and show results
    End if

    Author Comment

    hi ramuncikas,
    thanks for the help,
    it gives me a compiler error,, any idea why?
    Compilation Error
    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30201: Expression expected.

    Source Error:


    Line 2:  
    Line 3:  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Line 4:          If DropDownList1.SelectedValue="*" and DropDownList2.SelectedValue<>"*" OR
    Line 5:  DropDownList1.SelectedValue<>"*" and DropDownList2.SelectedValue="*" Then
    Line 6:              'report error

    Source File: C:\Documents and Settings\amir\My Documents\Visual Studio 2005\WebSites\YSTR\Default4.aspx    Line: 4

    LVL 14

    Accepted Solution

    correction -  you don't need new line after "OR" or add the underline sign "_" if you want to split IF statement inti two lines:

    If (DropDownList1.SelectedValue="*" and DropDownList2.SelectedValue<>"*") OR _
    (DropDownList1.SelectedValue<>"*" and DropDownList2.SelectedValue="*") Then
       'report error
       'query database and show results
    End if

    Author Comment

    thanks alot, it works :d

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now