且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在selectedindexchanged事件中使用下拉列表选定值

更新时间:2023-02-11 20:31:12

尝试 DropDownList.SelectedIndex属性 [ ^ ] - 应该是一个盯着选定的指南更改。
Try DropDownList.SelectedIndex Property [^] - should be a guide to get stared with selected change.


下面的代码需要写在下拉列表的selectedindexchanged事件中。

The below code needs to be written in the selectedindexchanged event of the dropdown.
ComboBox cmb = (ComboBox)sender;
if (cmb.SelectedValue.ToString().Equals("Something")
{
    //Do Something
}





希望这会有所帮助。



Hope this helps.


基于这个事实TS提到填充数据库的下拉列表,SelectedValue实际上不太可能是一个字符串。

很可能,ValueMember绑定到数据库中记录的Id,DisplayMember必然会一些说明。

Based on the fact that the TS mentioned populating the dropdown from the database, it's very unlikely that SelectedValue is actually a string.
More than likely, ValueMember is bound to the Id of the record in the database, DisplayMember is bound to some Description.
object val = ((ComboBox)sender).SelectedValue; // returns the Id
string txt = ((ComboBox)sender).SelectedText; // returns the text visible on the screen for the selected item
object item = ((ComboBox)sender).SelectedItem; // returns the entire object from the datasource you have bound to the combobox



根据您的需要,使用其中任何一个来检索/显示更多数据。


Depending on your needs, use any of these to retrieve/show further data.