Hi Scott,
When the page is automatically posted back, after a user changes the
selected index of the DropDownList, you must add the DropDownList control to
the hierarchy of controls on the page once again. As you mentioned the
Page.Init event is the recommended place to do this. The post-back event
for the DropDownList isn't raised until after the Page.Load event has
completed and if the DropDownList is not part of the control hierarchy at
this time the event, of course, will not be raised.
One solution is to create a custom UserControl that contains both the
TextBox and DropDownList controls and simply toggle the Visible property of
each instead of dynamically adding the DropDownList control. This way the
DropDownList is always part of the control hierarchy:
public class TextToListInput : UserControl
{
private TextBox TextBox1;
private DropDownList DropDownList1;
private void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox.Visible = false;
DropDownList1.Visible = true;
// TODO: bind DropDownList1
}
private void DropDownList1_SelectedIndexChanged(object sender, EventArgs
e)
{
// TODO: handle DropDownList1_SelectedIndexChanged event
}
}
Create an .ascx file for the UserControl and define two controls, a TextBox
named, "TextBox1" and a DropDownList named, "DropDownList1". Place them
wherever you'd like:
<asp:DropDownList runat="server" Id="DropDownList1" visible="false"
OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged" />
<asp:TextBox runat="server" Id="TextBox1" AutoPostBack="true"
OnTextChanged="TextBox1_TextChanged" />
As a side note, your users might experience delays having to request a page
with 100 controls every time they change a single text box. To improve the
user experience you might want to enable SmartNavigation or try a solution
that involves the 2.0 framework with ATLAS:
http://msdn.microsoft.com/msdnmag/is...t/default.aspx
- Dave Sexton
"scott blood" <sc*********@hotmail.comwrote in message
news:uI**************@TK2MSFTNGP04.phx.gbl...
Hello,
Can someone help me out here before i go bald or turn to drink to solve
all my problems.
I have a text box on a WebForm that when text is entered and the user
moves away from the control, the postback occours which then replaces the
TextBox with a Dynamically created DropDownList which contains a list of
items based on the text entered by the user.
So far all good, everything works...
Anyhow, the Dynamically created DropDownList's SelectedIndexChanged event
is hooked upto an event as follows.
MyListBox.SelectedIndexChanged += new
EventHandler(MyListBox_SelectedIndexChanged);
But the problem is that when the selected index is changed, nothing fires,
the DropDownBox simply dissapears off the WebForm, which i was expecting
as AutoPostBack is set to true to enable the SelectedIndexChanged event to
fire.
Anyone have any ideas what is happening here.
I have read lots of articles stating that dynamic controls should be
created in the OnInit event of the page, but this is not any good to me
because there could in theory be hundreds of these drop down lists on a
WebForm, basically dependant on the filtering level the user whats to go
down to.
Just so no one says 'Why would you want so many DropDownLists' on a form,
this is a financial application for the intranet which is handling
millions and millions and millions of rows of data which need filtering
down to the last penny.
Cheers guys :)