下拉列表选择值

本文关键字:选择 下拉列表 | 更新日期: 2023-09-27 18:27:18

我有一个下拉列表。我希望每当用户选择印度时,它都应该转到页面的下一部分,如果它选择的不是印度,它应该转到其他部分。而且,它应该只在选中复选框的情况下前进,否则它应该要求先选中复选框。请参阅代码以供参考:-

<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>

另请参阅复选框代码:

<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>

另请参阅按钮代码

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

按钮点击事件

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);
    }
}

下拉列表选择值

为DropDownList添加AutoPostBack为true,并添加选定的索引更改事件,如下面的

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

在"ddlCountry_SelectedIndexChanged"代码中,您可以检查所选的值、复选框值,并根据这些值执行任何您想要的操作。

代码

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" >

您选择的索引更改事件将类似于此

 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);
       }
}