繁体   English   中英

在第一个下拉列表中选择该项目时,如何从第二个下拉列表中删除该项目?

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

对于这个转换应用程序,当用户从第一个下拉列表中选择“十进制”时,我希望应用程序从第二个下拉列表中删除“十进制”,因为拥有该选项没有任何意义。 我使用属性中的项目集合来填充下拉列表中的项目。 这是我为 aspx.cs 文件编写的代码:

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 = "";
        }


    }
}

在 ddlSource_SelectedIndexChanged 事件中执行此操作

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

您需要使用SelectedIndexChanged事件,还要确保ddlSourceddlTarget具有AutoPostBack="true"属性。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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