473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SelectedIndex not firing

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 :)
Jul 21 '06 #1
2 2339
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 :)


Jul 21 '06 #2
Dave,

Thanks for your invaluable help, you have made my life a lot easier.

Regards

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uK**************@TK2MSFTNGP04.phx.gbl...
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 :)



Jul 24 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ross kerr | last post by:
Hi all, I have a control that extends the ComboBox object. It updates the selected item based on what the user enters in the text area. In the OnLeave event of the combobox, the selected...
3
by: Nathan Sokalski | last post by:
I am working on a simple user control composed of 3 DropDownLists that will be used to select Dates. The purpose of the control is to all the user to choose a date using dropdown lists but not need...
6
by: Nathan Sokalski | last post by:
I am writing a User Control that uses 3 DropDownLists. When I attempt to access the SelectedIndex property it incorrectly reports the value selected by the user. Why is this? Here is my code,...
5
by: Joe | last post by:
Hi I am adding a class to a ComboBox - and all is fine except that I fill the combobox on the Form_Load Method and it causes the method private void comboBox2_SelectedIndexChanged(object...
2
by: Lav KG | last post by:
Hi, I'm populating a RadioButtonList on the fly.It is successfully getting populated but when I click any of items it is not firing SelectedIndexchanged event, SelectedIndex is always -1 and...
4
by: afetsch | last post by:
It's been quite awhile since I've written anything in JavaScript, so please pardon my ignorance if I'm missing the obvious. The following code works fine in Firefox, but not in IE (unless I include...
1
by: Jason Wilson | last post by:
I have two dropdownlists that are bound to the same datasource and I have a couple of questions: 1) Because they are bound to the same datasource, I am assuming that they only make 1 round trip...
3
by: Alec MacLean | last post by:
Hi, I have a couple of win forms where I am editing values that are stored in a SQL database. I'm using the listbox control to hold the data object each form interacts with. Each object is...
1
by: marcbb | last post by:
Hi all, I have a really strange problem working with Dropdownlists in a DataGrid. I'm trying to preselect some values from the DropDownlist for each row in the DataGrid, but it seems that...
18
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.