DropDownList duplicates "bug" / feature / problem

R

RayLopez99

This seems to be a popular newbie issue, but all the answers I've seen
involve complicated SQL queries and data binding--mine is much more
simple.

How do you eliminate duplicates in a DropDownList in ASP.NET, using C#
as the coding language? Below is enough code to let you see what I'm
doing: I have two text boxes, and depending on whether "Add" or
"Multiply" is selected in the DropDownList1 control, the output text
box shows either the two text box contents added, or multiplied,
respectively, when a user clicks on Button1.

It's just an annoyance (the code works fine) since the DropDownList
shows "Add" and "Multiply" no less than three times. I had dragged
and dropped the DropDownList control from the ToolBox using the Wizard
in Visual Studio 2008. I also clicked "edit Items" (click on tiny
arrow for control) to indicate "Add" or "Multiply" for the members,
but even if I don't do this step you get three values (of "Add" and
"Multiply") everytime, duplicated.

Thank you.

RL

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

if (this.IsPostBack == false)
//if this.IsPostBack == false, the page is being created for the first
time and it's safe to initialize it
{
// strange, but commenting out these next two lines does NOT change
the program!
// Visual Studio 2008 apparently //initializes this behind the scenes
automatically?
DropDownList1.Items.Add("Add");
DropDownList1.Items.Add("Multiply"); //
}

}

protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "Multiply") //
DropDownList1.SelectedItem.Value
{
decimal num1 = Decimal.Parse(TextBox2.Text);
decimal num2 = Decimal.Parse(TextBox3.Text);
decimal ans = num1 * num2;
TextBox1.Text = ans.ToString();
}
if (DropDownList1.SelectedItem.Text == "Add") //now way of
picking DropDownList1.SelectedItem.Text == "Add"
{
decimal num1 = Decimal.Parse(TextBox2.Text);
decimal num2 = Decimal.Parse(TextBox3.Text);
decimal ans = num1 + num2;
TextBox1.Text = ans.ToString();

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top