Is there a trick to getting a SlectedIndexChanged event to fire on a DropDownlist control?
No matter how I select an item this event does not fire. I ahev checked my code, I can do even handling for any of the other controls in the form but nothing on this DropDownList.
Thansk in advance. |
| fhunter Sunday, September 11, 2005 5:49 PM |
Hi fhunter,
Have you tried getting it to work in a fresh project? 1) Add a ComboBox 2) Add some list items by selecting the ComboBox, clicking on Items in the property grid, and then writing a couple of lines 2) Set the DropDownStyle to DropDownList 3) Double-click the ComboBox 4) Add in the SelectedIndexChanged event handler: MessageBox.Show("Test"); 5) Run
When you select one of the items does the MessageBox show up? If not, then it probably isn't your code causing the problem. If so, then in your app that isn't working, you might want to make sure that the event handler is attached to the correct ComboBox's SelectedIndexChanged event. Let me know if the above doesn't work.
Thanks, Scott Morrison WindowsForms PM Microsoft Corp. |
| Scott_Morrison Tuesday, September 20, 2005 8:14 PM |
Hi fhunter,
Have you tried getting it to work in a fresh project? 1) Add a ComboBox 2) Add some list items by selecting the ComboBox, clicking on Items in the property grid, and then writing a couple of lines 2) Set the DropDownStyle to DropDownList 3) Double-click the ComboBox 4) Add in the SelectedIndexChanged event handler: MessageBox.Show("Test"); 5) Run
When you select one of the items does the MessageBox show up? If not, then it probably isn't your code causing the problem. If so, then in your app that isn't working, you might want to make sure that the event handler is attached to the correct ComboBox's SelectedIndexChanged event. Let me know if the above doesn't work.
Thanks, Scott Morrison WindowsForms PM Microsoft Corp. |
| Scott_Morrison Tuesday, September 20, 2005 8:14 PM |
Hi I have the similar problem w/o the event firing for dropdownlist when i pass a function at selected change event..any further help pls!
thanx |
| pkrish2 Wednesday, January 18, 2006 6:08 PM |
I'm having a similar problem where no events are firing (_Click, _SelectedIndexChanged, etc.), but this is only happening with one project. Other solutions I'm working on will click and fire perfectly. Copying working code or setting up simple test pages to test the events in the solution with the problem doesn't help. This is VisualStudio.NET 2003 with .NET 1.1
Thanks to anyone who has a potential solution.
|
| B. Schwartz Wednesday, January 25, 2006 3:47 PM |
I run into this bug occasionally. Today, I fixed it by fixing up some of the page HTML.
In my case, I had had a table nested within a table. I ended up deleting the outer table and somehow ended up with an orphan <strong> tag above my <table> tag. In the HTML window, this is underlined in red as invalid but, of course, you never see this if you are editing in Design view and just looking at that and your compiler output.
Once I deleled the bad nesting, the event fired normally.
So, start by checking your HTML. |
| Dave Hunt Thursday, January 26, 2006 11:21 PM |
I have had and am again havingthis same bug of ddl SelectedIndexChanged not firing.
When I select a new value on the ddl, the page posts back, the Page_Load function is invoked as PostBack, but the Event handler is never called. I have checked the handler logic in InitializeComponent; it is fine.
Yes, Autopostback is set true.
I use this event all the time. It works on most pages with no problem.
It will not work on the current page I am on, no matter what I do.
I am working in VS, .Net Framework 1.1, developing a webform. All other controls on the webform work fine - buttons, checkboxes and datagrid select.
There are no visible HTML errors on the html page.
I have no clue what else to look at or try.
I would greatly appreciate any insight or direction on where to go to get some actual help with this.
tim
|
| tim bickel Friday, January 27, 2006 2:51 AM |
We had
same kind of a problem yesterday: the SelectedIndexChanged event of a
radiobuttonlist did not fire, no matter what. But the answer was
simple, luckily. We had <form> tags in two places in our page,
first one on the main .aspx page and the second one on one of the user
controls of that .aspx page. We removed the <form> tag from the user control and now the radiobuttonlist works fine! - Proposed As Answer byvinod.kulkarni Tuesday, May 19, 2009 7:28 AM
-
|
| thuja Thursday, February 02, 2006 12:03 PM |
Run the sql andmake sure yourDataValueField returns Unique values. |
| HemaDevi Monday, March 13, 2006 7:45 PM |
Hello,
I have found that when I cut a control off of a form in design view and then paste it inside another container control (eg a GroupBox) that the Handles clause of my pre-existing event code gets stripped off. Since that is at the end of the method declaration, I usually end up pulling my hair out for a while until I think to check that. |
| j2associates Thursday, March 16, 2006 5:56 PM |
I too was experiencing what others have reported - the postback occurs, but the SelectedIndexChanged event is not called.
I think I've found a definitive cause for this and a workaround.
It seems that this event will only fire if the Value propertychanges. A bit of a bummer if you want different items to return the same value. (I got into this because the same timezone is called different things in different parts of the world.)
The"quick and dirty" code fragments below showthe workaround
To load the list (unbound)
N = 0 While dr.Read 'dr is a DataReader lstTimezones.Value = N.ToString & "|" & dr(0) ... additional code N = N +1 End While
In the SelectedIndexChanged event:
ValueSplit()As String = Split(lstTimezones.SelectedValue,"|") TimezoneCode = ValueSplit(1)
Is this an HTML thing or a Microsoft thing? I'd be interested in knowing. - Proposed As Answer byTom von Alten Wednesday, April 08, 2009 5:45 PM
-
|
| Reg J Wednesday, August 09, 2006 9:26 PM |
Hi,
I had the same kind of problem. Hema's solution worked out.
Thanks,
Chakri. |
| Kalyan Chakravarthy Tuesday, October 03, 2006 11:03 PM |
I had the same problem but whenI made auto postback property true, it worked
Thanks
Sree |
| Sreeya Monday, October 09, 2006 9:31 PM |
Oh !!
hours of testing and i was finally able to figure this out.
I had EnableViewState turned off with my .aspx page header.
when the ViewState is not saved it reverts back to the First Item as the Selected Index. Selecting the First Item then does not trigger a SelectedIndexChange Event.
make sure EnableViewState is true both on the control and on the page.
Manpreet. |
| Mon2 Monday, December 11, 2006 1:10 AM |
ya Manpreet's solution works fine .............
even I was facing the same problem & enabling Viewstate Works through it..
ThanxAll :-)..
Inderdeep.. |
| Rumy....._ Thursday, March 01, 2007 4:57 AM |
I also had the same prob ... now its fine..
|
| DeepthiPinto Saturday, March 17, 2007 6:39 AM |
Same problem here. SelectedIndexChanged was not firing after initial page load. Everything OK after one Page postback ???
:::: Finally solved this by removing something called "EnableSmartPaging=true" (or something very similar) in the aspx page header.
took me 4 hours to find this one.... |
| AxelG Wednesday, March 21, 2007 4:31 PM |
I met the similar problemwhen I was creating the user control which contains a ddl.
In testing, i found that the ddl can't postback the value I selected. I checked ViewState is set ture.
What's funny is that I have another user control with the same code and same entironment, that one works fine...
And, I put the control into another test page, it works fine also -.-
Now the problem seems to be fixed, but I don't knowwhy and whether it will occur again or not...
God bless me... |
| lyymax Tuesday, April 17, 2007 8:23 AM |
I had the same problem, and after 5 long hours i found the solution.
For theSelectedIndexChanged to be triggered, the drop down that triggers it has to have the same ID before and after the postback. Now, if you create the drop down list in the server side and do not give it an ID, then it would get an ID automatically from the .NET. The problem is when the ID it gets after the postback is not the same ID it got before the postback. This could be due to many things.
the solution: ddlSomething.ID = "ddlSomething"; |
| Noam11 Sunday, April 29, 2007 3:39 PM |
I had this problem in a user control. It turned out that the page where the control was used was not testing for postbacks, and was doing some databinding that messed up the list control. |
| Jim Sn Friday, June 01, 2007 3:36 AM |
I had the same problem. SelectedIndexChangednot firing on my project. Then ichanged dropdownlist autopostback property true; then it works fine know. |
| Ferrero DBGM Saturday, June 23, 2007 10:56 AM |
AutoEventWireup must be true at all times for this to work:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>
and / or
<%@ Master Language="C#" AutoEventWireup="true" Inherits="MasterPage" Codebehind="MasterPage.master.cs" %>
and / or
<form id="form1" runat="server" AutoEventWireup="true" >
You get the idea.
|
| Dynamo321 Friday, July 27, 2007 4:21 PM |
Jeeesshhhh!
That took a while to find!
Fixed it with:
int i=0;
DropDownList ddTPS = new DropDownList();
foreach (DataRow dr in TPSData.Rows)
{
ddTPS.Items.Add( new ListItem(dr["TPS"].ToString(), i /*Be sure this the PkId or a unique value "*/ );
i++;
} |
| cs96ai Wednesday, August 22, 2007 9:10 PM |
| Ferrero DBGM wrote: |
| I had the same problem. SelectedIndexChangednot firing on my project. Then ichanged dropdownlist autopostback property true; then it works fine know. | | Thank you sooooo much...this finally fixed it for me - Proposed As Answer byBusezgi Monday, April 20, 2009 2:18 PM
-
|
| zyxockjm Thursday, October 04, 2007 6:29 PM |
Please Enable AutoPostBack,it may solve problem |
| siuddin Thursday, November 08, 2007 6:34 AM |
OK. I did everything that everyone in the forum said. Set autopostback="true" on ddls. EnabledViewState everywhere. Set AutoEventWireUp="true" at page level.
Nada folks. Since the value on each item is the same, in this case blank. It would not fire.
My solution was as follows.
Dim ictr As Integer = 0
Dim ps As Char = "#"
Try
sqlConn.Open()
dbReader = sqlCmd.executeReader()
Select Case dbReader.HasRows
Case True
ddlS.Items.Add( New ListItem("Select Page Name", ""))
While dbReader.Read()
' If this page is already on the page table,
' do not put the page into the dropdown
ictr += 1
Select Case PageOnFile(dbReader.Item("PageName"))
Case False
Select Case IsDBNull(dbReader.Item("PagePath"))
Case False
ddlS.Items.Add( New ListItem(dbReader.Item("PageName"), dbReader.Item("PagePath") & ps & ictr.ToString()))
Case True
ddlS.Items.Add( New ListItem(dbReader.Item("PageName"), ps & ictr.ToString()))
End Select
End Select
End While
End Select
Catch ex As Exception
Finally
sqlConn.Dispose()
dbReader.Close()
End Try
ddlS.Enabled = True
ddlS.SelectedIndex = 0
This appended #n(n) to each value, thus giving each value a unique value.
Then to strip the value,
Dim ddl As DropDownList = sender
Dim ddlsv As String = ddl.SelectedValue()
Dim ix As Int16 = ddlsv.IndexOf("#")
ddlsv = ddlsv.Substring(0, ix)
tbPagePath.Text = ddlSiteName.SelectedValue() & ddlsv
Now it works.
Sheesh. 4-5 hours on this. What a waste. |
| TomAtAbilities Tuesday, November 27, 2007 2:55 PM |
Ok, I tried everything and still no go. All my values and ids are unique, autopostback=true, enableviewstate=true and nothing. Here's what I got: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleViews.aspx.cs" Inherits="MultipleViews" EnableViewState="true" %> // aspx ... <asp  ropDownList ID="DropDownList1" AutoPostBack="true" EnableViewState="true" runat="server"></asp  ropDownList> <asp:MultiView ID="MultiView1" runat="server"> <asp:View ID="xView" runat="server"> ... </asp:View> <asp:View ID="yView" runat="server"> ... </asp:View> <asp:View ID="zView" runat="server"> ... </asp:View> </asp:MultiView> ... // aspx.cs protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged); for (int i = 0; i < MultiView1.Views.Count; i++) { DropDownList1.Items.Add(new ListItem(MultiView1.Views  .ID)); } MultiView1.ActiveViewIndex = 0; } } void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { throw new Exception("The method or operation is not implemented."); } My only recourse was to change the Page_Load code: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged); for (int i = 0; i < MultiView1.Views.Count; i++) { DropDownList1.Items.Add(new ListItem(MultiView1.Views  .ID)); } MultiView1.ActiveViewIndex = 0; } else // add this { if (MultiView1.ActiveViewIndex != DropDownList1.SelectedIndex) MultiView1.ActiveViewIndex = DropDownList1.SelectedIndex; } } Seems hokey but I could not get anything else to work. Any ideas? (using VS 2005, asp.net, C#) |
| rcimba Friday, November 30, 2007 4:16 PM |
Thanks a lot Friends,
Solved the problem by setting AutoPostBack value to TRUE in DROP DOWNLIST PROPERTIES. |
| vijayanand_mohan_903ca4 Tuesday, January 29, 2008 11:43 AM |
I had an issue with DropDown's not firing events when I put them on a page that had a Master page. If I got rid of the Master page the events worked fine.Turns outwe had some vbscript in the header of the Master page file which must have screwed up scripting for the drop down controls:
< script language="vbscript" type="text/vbscript" src="somescript.vbs"></script>
Droppingan empty javascript function before the vbscript fixed it andthe drop downs started firing events properly. So if none of the above solutions seem to work, see if you have any vbscript in the header and drop some javascript before it:
< script language="javascript">
function emptyfunc()
{
}
</ script>
<script language="vbscript" type="text/vbscript" src="somescript.vbs"></script>
hope that helps |
| bradhi Tuesday, March 18, 2008 7:50 PM |
I had the same problem, the SelectedIndexChanged event not fired. I was using a Dropdownlist in a PagerTemplate fill with Documents Number. This pager is in a DetailsView. Autopostback was true since the beginning and I try many things that people wrote here. I found something about the EnableViewState property. To fire the event I must set the EnableViewState of my DetailsView to FALSE and the EnableViewState of my DropDownList to TRUE. Really strange, but now the event fired! Good news for me  hope it will helps |
| ForceMagic Thursday, April 03, 2008 1:35 PM |
Is there any trick to getting a SelectedIndexChanged event to fire on a DropDownlist control in ASP.NET 2008 application?
please help me to solve the problem
Thank in advance. |
| rajesh83 Saturday, May 17, 2008 7:35 AM |
This worked for me:
I created and and added the list control to its container in the in the container's CreateChildControls method. It seems to be a matter of "when" in the lifecycle that you connect controls that collect user responses... |
| joeslide Tuesday, June 10, 2008 1:19 PM |
It seems that if the DDL elements values are identical, the SelectedIndexChanged event is not fired... it's the kind of stupid mistakes that gets really painful to find out .
|
| Etienne Giust Friday, July 11, 2008 1:16 PM |
If creating a module for a DotNetNuke site, go into the module Settings after you install it and set the Cache time to zero. It defaults to 60 seconds. If it's set to 60 seconds, the events won't fire when a normal user uses the control. They WILL fire if a logged in superuser uses the control. Applies to buttons as well as dropdownlists. I keep running into this over and over again. Very annoying. |
| Yvette Friday, July 11, 2008 8:54 PM |
This was really helpful. The VBScript somehow is disabling the autopostback events for Drop Down boxes. By droping an empty javascript worked. Also my buttons were also disabled the same way but was able to fix them before this solution by using this in page_load
btnBoth.Attributes.Add("OnClick", "btnBoth_clicked");
|
| EngineByte Saturday, August 16, 2008 3:57 AM |
I have previously used DropDownLists for ASP.NET but now I want to use the same feature for a desktop application. Why is the dropdown list not available to me.
I am using VS2008 and the options that I have are DomainUpDown andListBox !!
I am not sure how to use these features. I want to display the results of a stored procedure in a dropdown list. Can someone please advice me 
Kind Regards
Michelle |
| Elmo_C_ Wednesday, August 20, 2008 8:24 AM |
I had also same problem, when I the AutoPostBack=true then it work nicely, by default it may have EnableViewState=true but not AutopostBack=true. So check ur component what you want to fire with the event. |
| muqim Wednesday, August 27, 2008 6:17 PM |
| vijayanand_mohan_903ca4 wrote: |
|
Thanks a lot Friends,
Solved the problem by setting AutoPostBack value to TRUE in DROP DOWNLIST PROPERTIES. | |  Worked indeed! |
| thomasvdb Thursday, October 16, 2008 7:10 AM |
It was very helpfull. I solvedt it by setting AutopostBack=true . Tks dude!
|
| Lucas_correa Wednesday, November 19, 2008 10:33 AM |
in ASP.NET there is no need to set the property AutoPostBack="true" to get this event fired. from what i have learned during my trials, any postback will trigger the SelectedIndexChanged event for a ddl. and when there is more than one ddl on a page and u r settin' some variable's value defined in class scope on SelectedIndexChanged event for a ddl, using AutoPostBack="true" might result in some loss of variable value caused by a post back for each ddl control. in some cases posting back with the help of a button and firing the event this way would be much wiser... this button will fire SelectedIndexChanged event for any ddls that has it's value changed on page at once. |
| Serdar Osman ONUR Thursday, February 05, 2009 8:38 PM |
Set the Controls AutoPostBack property to "true". |
| Jaya Kastury Monday, April 06, 2009 7:13 PM |
It seems that this event will only fire if the Value propertychanges....
THANK YOU for that explanation of this misfeature in .NET. The OnSelectedIndex method only fires when the VALUE THAT'S SELECTED CHANGES. Hello, Redmond? We have a problem. I wanted, and expected to be able to, use just the index of a list with effectively anonymous values. Miscrosoft says "no." My workaround was to set the style of the list to white on white. |
| Tom von Alten Wednesday, April 08, 2009 5:37 PM |
aspx页面里有个form嵌在里面,把那个form去掉就可以了 |
| wangronghua43152 Sunday, April 19, 2009 12:46 PM |
Thanks Ferrero DBGM it works |
| Busezgi Monday, April 20, 2009 2:17 PM |
Hi there, I have tried ALL the suggestions mentioned above, and nothing seems to work. I can't understand why the SelectedIndexChanged event won't fire. I work in VS2003 with .Net 1.1 * AutoEventWireup is set to "true" in the Page header. * Enableviewstate and Autopostback are both set to true in the control. The DDL-id is set:
<asp:dropdownlist id="CategoryDropDown" Runat="server" OnSelectedIndexChanged="CategoryDropDown_SIC" AutoPostBack="True" EnableViewState="True"></asp:dropdownlist></td>
* All values in the dropdownlist are unique. * OnClick events work without problem, it's just the SelectedIndexChanged event that doesn't fire. The c# code looks as follows:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
PopulateDropDown(-1);
}
}
protected void CategoryDropDown_SIC(object sender, EventArgs e)
{
category = CategoryDropDown.SelectedIndex;
PopulateDropDown(category)
}
protected void PopulateDropDown(int category)
{
SqlConnection conn = null;
try
{
string storeProcedure = ConfigurationManager.GetConfigKey("stp_handoutmaterial_categories");
string ConnectionString = ConfigurationManager.GetConfigKey("CRM.ConnectionString");
conn = new SqlConnection(ConnectionString);
conn.Open();
SqlDataReader reader = GetSqlReader(storeProcedure, conn);
CategoryDropDown.DataSource = reader;
CategoryDropDown.DataValueField = "new_category";
CategoryDropDown.DataTextField = "new_category";
CategoryDropDown.DataBind();
if(category != -1)
CategoryDropDown.SelectedIndex = category;
}
catch (SqlException ex)
{
string error = ex.ToString();
ErrorLabel.Text = error;
}
}
I'm running out of ideas! Does anybody have any other suggestions for things I could try? Thanks, Maria - Proposed As Answer byYogi_virgin Thursday, August 06, 2009 5:20 PM
-
|
| Maria Elmvang Wednesday, April 29, 2009 2:22 PM |
Don't know if this helps any of you, but I thought I was having the same issue. What it turned out to be was that I was actually using an excentricsWorld "scscombobox" as the control. Once I changed the control type to a standard ASP dropdownlist, the SelectedIndexChanged event fires as expected...D'OH! |
| ekotek Wednesday, May 06, 2009 5:38 PM |
Good, I am not the only one with this problem. Or bad, take your pick.
I see the postback come into the page, but, the selectedIndexChanged event is not fired when the value is the same.
Any work arounds for this?
Thanks, Scott |
| Scott G Vickery Wednesday, June 24, 2009 9:43 PM |
I spent 5 hours trying to get this problem fixed. Dropdownlist's onselectedindexchanged wouldn't fire at all, nor ImageButton's OnClick event. However, for odd reason LinkButton's OnClick worked.
Anyway, my solution was ridiculous. It turned out I had three "<form>" tags in the page. One in the Master Page and two in the current page. I took the two from the page, and WALLA it worked!
I hope this saves people from wasting their valuable time.
Yosep |
| yosepkim Wednesday, July 22, 2009 9:28 PM |
Dear Friends, Enable the "AutopostBack" attribute works for me in ListBox. Thanks for your help. :) |
| Leva T Tuesday, August 04, 2009 10:31 AM |
I also tried all the options mentioned on this page but nothing worked.
The dropdownlist was in the pagertemplate of the gridview, and I disabled the EnableViewState of Gridview and to my surprise it worked!!!
Not sure why is that so...
yogi
|
| Yogi_virgin Thursday, August 06, 2009 5:23 PM |
What I was doing was populating the dropdown on pageLoad and could not get the selectedIndexChanged to fire. As soon as I checked for postback and only populated when IsPostback was false, It worked. The dd was repopulating with the same info and selection every time the page loaded thereforeno change ( I think)
Kev |
| kevinduk Friday, August 21, 2009 1:05 PM |
I think i got the Answer:
On the Event property of the dropdown listbox in the SelectedIndexChanged field choose either "page_load" or the "name_SelectedIndexChanged". |
| yanirdz Tuesday, August 25, 2009 8:18 AM |
Setting autopostback = true soved my problem! Thanks!!!!! |
| ApexM Tuesday, August 25, 2009 7:35 PM |
I too was experiencing what others have reported - the postback occurs, but the SelectedIndexChanged event is not called.
I think I've found a definitive cause for this and a workaround.
It seems that this event will only fire if the Value propertychanges. A bit of a bummer if you want different items to return the same value. (I got into this because the same timezone is called different things in different parts of the world.)
The"quick and dirty" code fragments below showthe workaround
To load the list (unbound)
N = 0 While dr.Read 'dr is a DataReader lstTimezones.Value = N.ToString & "|" & dr(0) ... additional code N = N +1 End While
In the SelectedIndexChanged event:
ValueSplit()As String = Split(lstTimezones.SelectedValue,"|") TimezoneCode = ValueSplit(1)
Is this an HTML thing or a Microsoft thing? I'd be interested in knowing.
I realize that is a few years late, but I wonder if this scenario is the reason why the TextChanged event exists - to allow the same value, but different text?
David H |
| dch3 Thursday, September 03, 2009 8:54 PM |
Hey, I had the problem too where the postback was occuring but the SelectedIndexChanged was not firing for a dropdownlist control. I had set AutoPostBack to true and was using the code behind to bind the event. After about an hour I worked out what was going wrong for me, the codebehind was only binding the event when the control was originally loaded, on the postback it wasn't being bound so wasn't firing.
protected
void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
log(
"Postback " + e.ToString());
// This is the line I had to add to get it to work ddlColours.SelectedIndexChanged +=
new EventHandler(ddlColours_SelectedIndexChanged);
}
else
{
log(
loadDDLItems();
Step = 1;
}
}
Hope this helps someone AndySB
"Pageload"); |
| AndySB Thursday, September 24, 2009 9:20 PM |