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.
Related
ive got a ddl in a gridview with two values: 0 = no and 1 = yes. I've also got an event on the dll SelectedIndexChanged so when the user selects 1 then it shows a panel with a form in it. the form has two buttons: submit and cancel.
what i'd like to do is when the user selects cancel reset the ddl in the gridview back to 0.
I have an event on the cancel button and can hide the panel with the form but I'm not sure how to find the ddl that triggered the event to show the panel. do i need to use the row datakey such as the id?
When your SelectedIndexChanged event occurs, note down the row index - in which the DropDownList is present - for example saving the row index in a hidden field. Then when the cancel Button is clicked, use that hidden field, extract the row index, find the DropDownList in GridView in that row and reset it.
Hint: Use Control.Parent to find the host of a control, for example CType(mydropdown1.Parent, GridViewRow).RowIndex
However doing all this through JavaScript would be much better, in this way your user would NOT experience the server round trips on each click, but obviously requires good grip on JavaScript.
Respected sirs
I am using a DropDownList control and binding it to a LinqDataSource.
According to the selection of the mentioned DropDownList, I need to bind another DropDownList control.
I have handled the SelectedIndexChanged event for first DropDownList. When this SelectedIndexChanged occurs, page is getting refreshed. To prevent page from beend refreshed, I am using an UpdatePanel control. But, still the second DropDownList not loading.
Please give me a solution.
Thanks
Saravanaa
Place the second DropDownList inside the UpdatePanel.
You may want to consider using CascadeDropDown too.
On my asp.net page, I have several DropDownLists.
I also have a Repeater.
In the ItemDataBound event I want to get the value of these DropDownLists, to change the data in the Repeater. The SelectedValue of these DropDownLists is empty.
But after the ItemDataBound, the Page_Load is executed. There I can get the value of these DropDownLists.
Is there a solution to get the value when the ItemDataBound is executed.
thanks!
Filip
You need to data-bind these drop-down lists in the Page.Load event.
There're a lot of web controls that get their state or other details during load life-cycle (I had these kind of problems long time ago).
NOTE: When I say "State" I'm not talking about ViewState.
And why don't you do that data-bind after the load event?
Can you get the drop down lists' selected values in the page PreInit event? If so, store them in view state and retrieve them from view state during the repeater's item data bound event.
If that doesn't work, try adding a selected index changed event to each drop down. When the drop downs change, set a view state variable that you can retrieve during the repeater's item data bound event. If you have values to which you are setting the drop downs during page load, such as when reading from a database, use those values to directly set the appropriate view state variables.
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?
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.