SelectedIndex_ Changed not getting fired! - asp.net

SelectedIndex_Changed event occurs when selection changes, and selection is changed or not is decided by comparing dropdowns current selectedIndex and its value in viewstate.
When we changes dropdown's selected Item using Javascript, it does not change the viewstate's value (which is previously selectedIndex). Now if you change dropdown's selectedIndex to any new index it will work fine, but if you select the previously selectedIndex (which we reset using javascript), then postback will occur, but selectedIndex_changed function will not get fired because in viewstate of dropdown, selectedIndex is same as the Index we select after resetting it using javascript.
I need to look for some solution to tackle this situation.

This is by-design of the control. If you select the same index, then the index has not changed. If the index has not changed, then why would the SelectedIndex_Changed event fire?

Related

disabled dropdownlist does not keep selected value on postback

I'm getting a weird behavior with a dropdownlist when I trigger a postback.
If the dropdownlist is enabled, the selected value remains the same after a postback.
However, if the dropdownlist is disabled (via a javascript when user ticks a checkbox), then the selected value is reset to the first item in the list.
How come ?
you need to look up the value manually (probably store it in another field or so), and then set it yourself in the code behind. This is because if a control is disabled, the value would not be posted back to the sever.
Check out this question, and refer to the first answer by Pavan
how to get selected value of Disabled dropdown in c#
If you are trying to read the value of 2nd dropdown (disabled one) at
server, you will never be able to read the updated value, becuase data
in disabled controls will not be posted back to server from client

About DropDownList

Am using DropDownList controls in ASP.NET binding values from SQL table. Some control have only one value, some control have more than one value. The SelectedIndexChanged event is not fired which control have only one value.
I set AutoPostBack=true and 0th item as select. Even though that event is not fired for that control?
SelectedIndxChanged event fires only if you change your selection and your DDL has AUTOPOSTBACK true.
If you have only one value in DDL then you don't need this event , you can write your desire code after you bind the dropdown on server side itself.
If you are not binding DDL from server side you can write your code on or after Page_Load event there you'll get the selected index
SelectedIndexChanged only fires when the selection has changed. If there's only one item in the dropdown list, you can never get this to fire (there's nothing to change to).
That being said, you can add a blank item to the top of the list (index 0), with the following at the top of the code where you populate the dropdown list:
MyDDList.Items.Add("");
That way you CAN choose something even with your one item.

Event/postback cycle in dropdown

I know this might be an easy question but still:
I got a dropdown and a button on my website, autopostback is false on the dropdown and I use the "SelectedIndexChanged" event. When I pick an item from the dropdown nothing happens ofcoure, but when I click the button, the system somehow knows that the index has changed in the dropdown and it calls the SelectedIndexChanged event, where does it store this information?
Im guessing the events are being added to a list and then fired upon postback
The original state of the DDL is stored in viewstate. Upon postback, the new state and old state are compared and the event fired (or not) accordingly

Disabling Viewstate Causes selected dropdown values to default to -1

I don't get this. So I disable viewstate on my .aspx page. I then select options from 3 dropdown menus and then on submit check the values and they are all -1. I put ViewState back on and do the same, select values and now in the submit event they're all set to a valid value for the SelectedIndex.
I don't get this. I don't see how viewstate has anything to do with selecting values in a dropdown and then calling a server-side handler to pick up those selected values in order to do something with it and in order to get the actual valid SelectedIndex values that the user selected. I don't see why it would give me a -1 if I disable viewstate for the SelectedIndex for each of those 3 dropdownlist controls.
Either move your code that binds the dropdowns into Page_Init or surround them with
if (!IsPostPack) { ... }

Normal UpdatePanel behaviour? ASP.NET

Am I doing this correctly?
I have two dropdowns. The first is populated on form load, and it is a trigger for an updatepanel that contains the 2nd dropdown.
When I change the value in the first dropdown box, it triggers the 2nd one's updatepanel, which populates it.
However, when I select a value in the 2nd dropdownbox, it triggers itself and repopulates... so if I select the 2nd or 3rd item in the dropdown, it repopulates and selects the first item again.
Am I doing something wrong, or is this normal? Should I put the first dropdown into an updatepanel instead and have it trigger itself, and in its own trigger, populate the 2nd listbox?
It appears that changing the value in the second dropdown is causing a postback, which it then sets the value to the default or first value. Try setting the dropdown's AutoPostBack property to false and EnableViewState to true.
Use the ASP.NET Ajax Control Toolkit CascadingDropDown instead.
EDIT: You need to set your UpdateMode=Conditional and control when your UpdatePanel's should update, by calling UpdatePanel2.Update().
I had this problem once.
This is a problem for update panel plus dropdown autopostback
By default ViewState for page is stored,so you do not need to change ViewState for control.
First,Change your UpdatePanel's UpdateMode property to Conditional UpdateMode="Conditional".This ensures that you can control postback since your dropdown always postback when you set AutoPostBack to true.
Second is in your method which you bind datasource into dropdown,after databinding is done.Update your panel by calling Update method of UpdatePanel updPanel.Update(); by
dropdown.DataSource = source;//I presume
dropdown.DataBind();
updPanel.Update();
Hope this helps.

Resources