Simple one, but need help please. I need to populate a list box based on the values of a textbox. My sample.aspx code is as below
<div>
<asp:TextBox ID="TextBox1" AutoPostBack="true" runat="server"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
</div>
My sample .aspx.cs code is as below
protected void Page_Load(object sender, EventArgs e)
{
DataList("page");
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
DataList(TextBox1.Text);
}
private void DataList(String value)
{
DataTable dt = new DataTable();
dt.Columns.Add("City", typeof(string));
dt.Columns.Add("Country", typeof(string));
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["City"] = i.ToString() + value;
dr["Country"] = i.ToString() + "A";
dt.Rows.Add(dr);
}
ListBox1.DataTextField = "City";
ListBox1.DataValueField = "Country";
ListBox1.DataSource = dt;
ListBox1.DataBind();
}
The TextBox1_TextChanged is fired only if the control either goes out of focus or hit an enter or tab button on the TextBox. Any means to achieve it on the fly, eg. I will key in say "B", and I should be able to see the listbox immediately reflecting as 0B,1B,... 9B. The moment I add Ba, the listbox should modify as 0Ba,1Ba,... 9Ba.
Thanks and Regards,
cmr