Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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..
Posted
Comments
upendra shahi 4-May-15 5:59am    
comboBox.SelectedIndex = comboBox.Items.IndexOf("your choice");

Please check the aspx page code as below =
C#
<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>


Please check the code behind =

C#
protected void Button1_Click(object sender, EventArgs e)
	{
		DropDownList1.SelectedValue = TextBox1.Text;
	}
 
Share this answer
 
After databind you can find the value and set it.
C#
protected void dl_DataBound(object sender, EventArgs e) {
    dl.Items.FindByValue(val).Selected = true;
}
 
Share this answer
 
Combobox.SelectedValue = Textbox.text
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900