以编程方式更改下拉列表框中的所选项目 [英] programatically change selected item in dropdown listbox
本文介绍了以编程方式更改下拉列表框中的所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本框和一个下拉列表框。该文本框用于输入下拉列表框的新记录。下拉列表框用于从预先存在的记录中进行选择。我想要做的是使用文本框创建一个新记录,然后让下拉列表框更改为新创建的项目。我在其他代码中使用下拉列表框中的选定值,并且需要它在创建新记录时反映新记录的id。
有什么建议吗?
I have a text box and a drop down list box. The text box is used to enter new records for the dropdown list box. The dropdown list box is used to select from pre-existing records. What I want to do is to create a new record using the text box but then have the dropdown list box change to the newly created item. I am using the selected value from the drop down listbox in other code and need it to reflect the new record's id when a new record is created.
Any suggestions?
推荐答案
你可以这样做,
You can do something like this,
int k = 0;
//你喜欢的Textbox事件中
//inside your Textbox event you prefer
DropDownList1.Items.Add(new ListItem(TextBox1.Text,k.ToString()));
DropDownList1.DataValueField = k.ToString();
int count = DropDownList1.Items.Count;
DropDownList1.SelectedIndex = count - 1;
这篇关于以编程方式更改下拉列表框中的所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文