简体   繁体   中英

How to remove an item from the second drop down list when that item is selected in the first drop down list?

For this conversion application, when the user selects "decimal" from the first drop down list, I want the app to remove the "decimal" from the second drop down list, as it wouldn't make any sense to have that option. I used the items collection in the properties to fill in the items in the drop down list. Here is the code I have for the aspx.cs file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FirstAssignment
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void ddlSource_SelectedIndexChanged(object sender, EventArgs e)
        {   
        }
        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                String target = ddlTarget.SelectedItem.Value;

                if (ddlSource.SelectedValue == "Decimal") /// decimal to other formats
                {
                    int number = int.Parse(txtSource.Text);


                    if (target == "Binary")
                    {
                        String Base = Convert.ToString(number, 2);
                        lblOutput.Text = Base;
                        txtHistory.Text = number + " Decimal = " + Base + " Binary";

                    }
                    else if (target == "Hexadecimal")
                    {
                        String Base = Convert.ToString(number, 16);
                        lblOutput.Text = Base;
                        txtHistory.Text = number + " Decimal =  " + Base + " Hexadecimal";
                    }
                    else if (target == "Octal")
                    {
                        String Base = Convert.ToString(number, 8);
                        lblOutput.Text = Base;
                        txtHistory.Text = number + " Decimal =  " + Base + " Octal";
                    }

                } else if (ddlSource.SelectedValue == "Binary")  ///Binary to other formats
                {
                    String source = txtSource.Text;

                    if (target == "Decimal")
                    {
                        String Base = Convert.ToInt32(source, 2).ToString();
                        lblOutput.Text = Base;
                    }
                    else if (target == "Hexadecimal")
                    {
                        String Base = Convert.ToInt32(source, 2).ToString("X");
                        lblOutput.Text = Base;
                    }
                    else if (target == "Octal")
                    {
                        int Base = Convert.ToInt32(source, 2);
                        String Base1 = Convert.ToString(Base, 8);
                        lblOutput.Text = Base1;
                    }

                } else if (ddlSource.SelectedValue == "Hexadecimal")  ///Hexadecimal to other formats
                {
                    String source = txtSource.Text;

                    if (target == "Decimal")
                    {
                        String Base = Convert.ToInt32(source, 16).ToString();
                        lblOutput.Text = Base;
                    }
                    else if (target == "Binary")
                    {
                        int Base = Convert.ToInt32(source, 16);
                        String Base1 = Convert.ToString(Base, 2);
                        lblOutput.Text = Base1;
                    }
                    else if (target == "Octal")
                    {
                        int Base = Convert.ToInt32(source, 16);
                        String Base1 = Convert.ToString(Base, 8);
                        lblOutput.Text = Base1;
                    }

                }  else ///Octal to other formats
                {
                    String source = txtSource.Text;


                    if (target == "Decimal")
                    {
                        String Base = Convert.ToInt32(source, 8).ToString();
                        lblOutput.Text = Base;
                    }
                    else if (target == "Binary")
                    {
                        int Base = Convert.ToInt32(source, 8);
                        String Base1 = Convert.ToString(Base, 2);
                        lblOutput.Text = Base1;
                    }
                    else if (target == "Hexadecimal")
                    {
                        int Base = Convert.ToInt32(source, 8);
                        String Base1 = Convert.ToString(Base,16);
                        lblOutput.Text = Base1;
                    }
                }

            }




            txtHistory.Text += txtSource;
        }

        protected void btnClear_Click1(object sender, EventArgs e)
        {
            txtSource.Text = "";
            ddlSource.ClearSelection();
            ddlSource.SelectedValue = "--Select One--";
            ddlTarget.ClearSelection();
            ddlTarget.SelectedValue = "--Select One--";
            lblOutput.Text = "";
        }


    }
}

Do this in the ddlSource_SelectedIndexChanged event

protected void ddlSource_SelectedIndexChanged(object sender, EventArgs e)
{   
    ddlTarget.Items.Remove(((Dropdown)sender).SelectedItem.Value));          
}

You need to use SelectedIndexChanged event, also make sure that ddlSource and ddlTarget have AutoPostBack="true" attribute.

protected void ddlSource_SelectedIndexChanged(object sender, EventArgs e)
{
    if ((sender as DropDownList).SelectedValue == "Decimal")
    {
        ddlTarget.Items.Remove("Decimal");
        ddlTarget.DataBind();
    }
}

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