简体   繁体   中英

Dropdown list selecting value

I have a dropdownlist. I want whenever user selects India, it should go to the next section of the page and if it selects other than India, it should go to the some other section. And also it should only move ahead if the checkbox is checked, Otherwise it should ask to check the checkbox first. Please see the code for your reference:-

<asp:DropDownList ID="ddlCountry" runat="server" class="txtfld-popup" Width="251">
    <asp:ListItem Text="Enter your Country of Residences" Value="0"></asp:ListItem>
    <asp:ListItem Text="India" Value="1"></asp:ListItem>
    <asp:ListItem Text="USA" Value="2"></asp:ListItem>
    <asp:ListItem Text="Other" Value="3"></asp:ListItem>
</asp:DropDownList>

Also see the checkbox code:

<asp:CheckBox ID="checkcountry" runat="server" />
I confirm that I am a resident of the selected jurisdiction.
<div id="errordiv" runat="server" style="color: #cf060d; font-size: 12px;"></div>

Also, see the button code

 <asp:Button ID="btnSend" runat="server" ValidationGroup="VG" 
      OnClick="btnSend_Click" Class="button-form" Width="65" />

On button click event

protected void btnSend_Click(object sender, EventArgs e)
{
    if (!checkcountry.Checked)
    {
        errordiv.InnerHtml = "Please select the authorization checkbox to proceed.";
        return;
    }
    if (Page.IsValid)
    {
        // Response.Redirect("LegalPopup1.aspx"); 
        ClientScript.RegisterClientScriptBlock(this.GetType(), "ResponseDialog", "$(document).ready(function(){ResponseDialog();});", true);
    }
}

Add AutoPostBack as true for the DropDownList and add selected index changed event like below

<asp:DropDownList ID="ddlCountry" runat="server"  OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true" 

inside your "ddlCountry_SelectedIndexChanged" code, you can check the selected values, checkbox values and do whatever you want based on those values.

Code

protected void btnSend_Click(object sender, EventArgs e)
{
    if (checkcountry.Checked == true)
    {
        if (checkcountry.Checked == true && ddlCountry.SelectedIndex != 0 && ddlCountry.SelectedItem.Text == "India")
        {
            divForInida.Visible = true;
            divForOther.Visible = false;
        }
        else if (checkcountry.Checked == true && ddlCountry.SelectedIndex != 0 && ddlCountry.SelectedItem.Text != "India")
        {
            divForInida.Visible = false;
            divForOther.Visible = true;
        }
        else
        {
            //message for `Select a country of residence`
        }
    }
    else
    {
        //message for `check the checkbox`
    }
}
<asp:DropDownList ID="ddlCountry" runat="server"  OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true" >

And Your selected index changed event will look like this

 protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
      try
       {
          if (!string.IsNullOrEmpty(ddlCountry.SelectedItem.Text))
          {
            if (ddlCountry.SelectedItem.Text == "India")
            {                  
            }
            else 
            {                   
            }                
          }          
       }
       catch (System.Exception ex)
       {
         throw new MyException(" Message:" + ex.Message, ex.InnerException);
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM