如何使用Value Member选择组合框的索引 [英] How Do I Select Index of combobox using Value Member
问题描述
我使用字典填充组合框。每次索引更改时,我都会使用messagebox来显示组合框的值。示例输出是[100,Apple]
另一个[101,Banana]。我想要完成的是当我在文本框中输入100并单击按钮时,组合框索引将自动选择[100,Apple]的索引,如果我输入101,则再次单击该按钮,[101,Banana]的索引将是选择等等。我试图使用combobox.findstring(字符串)但无济于事。
我希望有人知道这个会告诉我正确的方向......
谢谢..
I populate combobox using dictionary. I use messagebox to show value of combobox everytime its index change. Example output is [100, Apple]
another [101, Banana]. What I want to accomplish is when I type 100 in textbox and click a button the combobox index will automatically select the index of [100, Apple] and if I type 101 then click the button again the index of [101, Banana] will be selected and so on. I tried to use combobox.findstring(string) but to no avail.
I'm hoping that someone who knows this will show me the right direction...
Thank you..
推荐答案
请检查aspx页面代码如下=
Please check the aspx page code as below =
<div>
<asp:textbox id="TextBox1" runat="server" xmlns:asp="#unknown"></asp:textbox>
<asp:button id="Button1" runat="server" text="Button" onclick="Button1_Click" xmlns:asp="#unknown" />
<asp:dropdownlist id="DropDownList1" runat="server" datasourceid="SqlDataSource1" datatextfield="Name" datavaluefield="Id" xmlns:asp="#unknown"></asp:dropdownlist>
<asp:sqldatasource runat="server" id="SqlDataSource1" connectionstring="<%
ConnectionStrings:LOC_MASTERConnectionString%> selectcommand = SELECT [Id],[Name] FROM [Fruit] xmlns:asp = #unknown >
< / asp:sqldatasource >
< / div >
ConnectionStrings:LOC_MASTERConnectionString %>" selectcommand="SELECT [Id], [Name] FROM [Fruit]" xmlns:asp="#unknown"> </asp:sqldatasource> </div>
请检查背后的代码=
Please check the code behind =
protected void Button1_Click(object sender, EventArgs e)
{
DropDownList1.SelectedValue = TextBox1.Text;
}
在数据绑定之后,您可以找到该值并进行设置。
After databind you can find the value and set it.
protected void dl_DataBound(object sender, EventArgs e) {
dl.Items.FindByValue(val).Selected = true;
}
这篇关于如何使用Value Member选择组合框的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!