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



对于此转换应用程序,当用户从第一个下拉列表中选择"十进制"时,我希望应用程序从第二个下拉列表中删除"十进制",因为拥有该选项没有任何意义。我使用属性中的项目集合来填写下拉列表中的项目。这是我为 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();
}
}

最新更新