下拉列表选择值

时间:2014-10-20 09:41:04

标签: c# asp.net

我有一个下拉列表。我希望每当用户选择印度时,它应该转到页面的下一部分,如果它选择除印度之外,它应该转到其他部分。如果选中复选框,它也应该只向前移动,否则它应该先要求选中复选框。请参阅代码供您参考: -

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

3 个答案:

答案 0 :(得分:1)

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

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

在你的&#34; ddlCountry_SelectedIndexChanged&#34;代码,您可以检查选定的值,复选框值,并根据这些值执行任何操作。

答案 1 :(得分:0)

代码

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

答案 2 :(得分:0)

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