Question

I have two drop down list.

public void DrpDwn_Cuntry()
    {
        if (!Page.IsPostBack)
        {
            MySqlCommand sql_country = new MySqlCommand("SELECT DISTINCT(Country) FROM Animals", cs);
            cs.Open();

            MySqlDataReader ddlvalue;
            ddlvalue = sql_country.ExecuteReader();

            ddlcountry.DataSource = ddlvalue;
            ddlcountry.DataValueField = "Country";
            ddlcountry.DataTextField = "Country";
            ddlcountry.DataBind();
            ddlcountry.Items.Insert(0, "Choose A Sanctuary");

            cs.Close();
            cs.Dispose();
        }
    }

And

public void DrpDwn_Res()
    {
        if (!Page.IsPostBack)
        {
            MySqlCommand sql_residents = new MySqlCommand("SELECT DISTINCT(Country) FROM Animals", cs);
            cs.Open();

            MySqlDataReader ddlvalue_residents;
            ddlvalue_residents = sql_residents.ExecuteReader();

            ddlcountry_Res.DataSource = ddlvalue_residents;
            ddlcountry_Res.DataValueField = "Country";
            ddlcountry_Res.DataTextField = "Country";
            ddlcountry_Res.DataBind();
            ddlcountry_Res.Items.Insert(0, "Choose Your Country");

            cs.Close();
            cs.Dispose();
        }
    }

I would like a message box to show if the two selected do not match. for example if the selected country a from first and country b from second message box shows. I know i am to use a If Else statement I am just not sure how to write it ?

Was it helpful?

Solution

you can use SelectedItem property of the DropDownList Control to achieve this.

1. get the SelectedItem of the first DropDownList.
2. get the SelectedItem of the second DropDownList.
3. compare both of the SelectedItem values using Equals() method.
4. if the items do not match , display an alert box using javascript as MessageBox is not bydefault avaialble in Webforms (in webforms it is good to use javascript alert) using following Syntax:

Response.Write(@"<script language='javascript'>alert('message here');</script>");

Complete Code: (Code Behind)

Try This:

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (!ddlcountry.SelectedItem.ToString().Equals(ddlcountry_Res.SelectedItem.ToString()))
        {
            Response.Write(@"<script language='javascript'>alert('Items do not match.');</script>");
        }
    }

OTHER TIPS

The message box is available in WinForms, the equivalent in WebForms would be the Alert through javascript (You will need to add the reference on jQuery library)

Here it goes:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js "
    type="text/javascript"></script>

<script>

  function compareAndAlert(){
   var value1 = $('#dropDownId').val(); // get the selected value of first dropdown
   var value2 = $('#dropDownId2').val(); // get the selected value of second dropdown
   if (value1 != value2){
     alert("The selected items do not match !"); // If the selected values are not equal display an Alert.
     return false;
   }
   return true;
 }

</script>

now call this method on the button click

<asp:Button ID="Button1" onclientclick="javascript:if(compareAndAlert()){}else{return false;}" runat="server" OnClick="Button1_Click" Text="Adopted Pet" Height="31px" Width="150px"/>

Update: This is basically validation and though the Sudhakar's answer is correct but it will lead to unnecessary postback even if the selected values do not match. These type of validations are best served clientside. Do check the update, i have changed the content in onclientclick event of your button

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top