简体   繁体   English

C#:无法从组合框转到下拉列表

[英]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. 我已经在VS Express 2013中为Windows开发了一个应用程序,用户可以在其中仅显示对象名称的组合框中选择其他方法。 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. 该程序中大约有50个。 The first part is the name, the other is the ID that the XML uses to find stuff. 第一部分是名称,另一部分是XML用于查找内容的ID。 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. 但是现在,我专注于将整个内容转换为网站,并使用VS Express 2013 for Web。

There is no combobox there, so im stuck using dropdownlist. 那里没有组合框,所以我使用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: 上面的使用“ AddRange”加载列表的方法不起作用,并且我尝试了约一百种东西,直到我最终以这种方式显示名称:

        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? 它说:“无法将blablabla类型转换为WebApplication4.Converter。怎么了?我有什么办法可以使组合框以良好的旧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. 欢迎来到Web开发和整个无状态的请求/响应机制的世界。 To understand this fully have a read up on the differences between windows and web development. 要完全理解这一点,请阅读Windows和Web开发之间的区别。

In webforms the controls are a lot more basic than the winforms ones. 在Webforms中,控件比Winforms控件更加基本。 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. 网络表单Dropdownlist.SelectedItem的类型为ListBoxItem-仅具有从dropdownlist控件中绘制和获取项目所需的最少字段-仅是ListItem中的文本以及是否已选中。 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? 那么,如何才能将所选项目映射回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. 您可以使用字符串值Dropdownlist.SelectedItem.Value进行某种查找,也可以使用Dropdownlist.SelectedIndex从源列表中的该索引中查找项目。

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! 祝好运!

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

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