简体   繁体   中英

C#: Trouble going from combobox to dropdownlist

I am still very new to programming, as im sure my question will show. I have spent the past two days utterly stuck, and i have not been able to find the answer to this anywhere. Might be because im just overlooking something so obvious that no one has had to ask, but here it goes:

I have made an application in VS Express 2013 for windows where the user chooses an alternative from a combobox displaying only the name of the object. From the choice i send the whole object to my "Converter Class".

        EU = new Converter("Enriched Uranium", "44");
        CO = new Converter("Coolant", "9832");
        BI = new Converter("Biocells", "2329");
        CB = new Converter("Construction Blocks", "3828");
        FR = new Converter("Fertilizer", "3693");
        GL = new Converter("Genetically Enhanced Livestock", "15317");

        object[] myArray1 = { EU, CO, BI, CB, FR, GL };
        comboBox1.DisplayMember = "name";

There are around 50 of these in the program. The first part is the name, the other is the ID that the XML uses to find stuff. They perform lots of stuff further on in the code, but this is the start:

        Converter a = ((Converter)comboBox1.SelectedItem);
        a.CallXml();
        a.taxPrice(comboBox2.Text);
        a.getNumber(textBox4.Text);
        a.getTax(taxrat);

And so on... I know its not exactly beautiful, and i am seeing a lot of ways to make it more effective after learing. But right now im focusing on converting the whole thing to a web site, and im using VS Express 2013 for Web.

There is no combobox there, so im stuck using dropdownlist. The above method of loading the list with "AddRange" did not work, and ive trid about a hundred things, until i finally get to display the names this way:

        List<object> myList1 = new List<object>();
        myList1.Add(EU);
        myList1.Add(CO);
        myList1.Add(BI);

        DropDownList2.DataSource = myList1;          
        DropDownList2.DataTextField = "name";
        DropDownList2.DataBind();

So far so good! Where i am now utterly stuck is at the point where i need the users choice to return the object and send it to the "Converter" class. This is the closes i feel ive come:

    protected void Button1_Click(object sender, EventArgs e)
    {
        object a = (Converter)DropDownList2.SelectedItem; 
    }

It says: "Cannot convert type blablabla to WebApplication4.Converter. How come? And is there any way i can make it to perform the same action the combobox did back in good, old winform?

What i want to do is that when the user chooses "Enriched Uranium", the program calls the "Converter class" with for example:

        EU.CallXml();

And so on.

Welcome to the world of web development and the whole stateless request / response mechanism. To understand this fully have a read up on the differences between windows and web development.

In webforms the controls are a lot more basic than the winforms ones. Really the binding is just one way - its just using your source data to get the info needed to draw the list. If you think about it it has to be like this, otherwise it would be having to pass your entire list of objects between the server and the clients browser which would be wasteful, and its hard in a stateless environment to know when to bind back etc.

The webforms Dropdownlist.SelectedItem is of type ListBoxItem - which just has the minimum fields needed to draw and get the item from the dropdownlist control - just the text from the ListItem, and whether its selected. Which is why you can't cast it back to your converter object.

So how can you get map the selected item back to your Converter? You can either use the string value Dropdownlist.SelectedItem.Value to do some sort of lookup, or use the Dropdownlist.SelectedIndex to find the item from your source list at that index.

You could do something like this

List<Converter> myList1 = GetConverters(); //todo - write a method to make the list    

DropDownList2.DataSource = myList1;          
DropDownList2.DataTextField = "name";
DropDownList2.DataBind();

and on the click event

protected void Button1_Click(object sender, EventArgs e)
{
   string s = DropDownList2.SelectedItem.Value; 

   Converter c = GetConverters().Find(x => x.name == s);

   //do whatever with the converter
}

disclaimer - none of this is compiled - so may have some errors. Also its definitely not the best way of doing it (no error checking + making the list of converters just to do the datasource bind is a bit wasteful), just to show you how it could be done.

One other warning - make sure you only do the datasource bind once - its possible if you do it without a check that the page hasn't posted back it could bind just before you try to get the value - meaning the first value will always seem selected. Good luck!

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