DropDownList reloading - asp.net

I have 2 dropdownlists on a WebForm. Dropdownlist1 controls what should display on DropDownList2.
When the page first loads I pass in a selectedindex of 0 to function to load DropDownList2.
The data loads perfectly.
However when I change the selectedindex on DropdownList1 which causes a postback to recalculate new Dropdownlist2 vales the populating fails. Even though dropdownlist is bound to new datasource that does have data in it.
The data loaded in inatial page load remains in DropDownlist2.
EnableViewState is = true on Dropdownlist2 and 1.
Im developing using asp.net 2.0
I have no idea of why this is failing and would be really grateful if anyone could shed some light on it.
Many Thanks
Tony

Why not use the Cascading dropdwn in the AJAX Control Toolkit? http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/CascadingDropDown/CascadingDropDown.aspx
If you don't want to do that, make sure that populating DropDown1 occurs within a postback check. Otherwise, the DropdownList1 will be repopulated and the SelectedIndex set to 0 on every postback.
if (!Page.IsPostBack)
{
//Populate DD1
}

Related

How to bind TextBox from DropdownList in ASP NET without page refresh

I have a dropdownlist control and a text box control in asp.net. dropdownlist is getting data from database. suppose some data are x-ray,USG,MRI and their value are 100,200,300. So if i select x-ray from the dropdown i want it's value 100 binds at the textbox without page refresh. How can i do this??? Please Help.
Using .net, to have something happen on the server side when something is selected from DropDownList, having property AutoPostBack=true is required. This means when you change the SelectedIndex, the page refreshes (postback), then the SelectedIndexChanged event is fired.
Theres a few approaches. In your page_load event, put everything you want to happen on the first page load in If Not IsPostBack Then ... End If block. Then when a PostBack happens, like DropDownList_SelectedIndexChanged, the code in If Not IsPostBack Then won't happen, because it IS a postback.
The above approach still has a page reload, but you can choose what happens when its this type of PostBack page reload. You may have javascript thats re-firing, though...
The second approach would be to have no postback event happen from the dropdownlist, and handle the Binding of the selected item in DropDownList to the TextBox with javascript.

Dynamically loaded dropdownlist control doesn't fire SelectedIndexChanged event

I have the control dropdownlist which has been loaded inside the template column of RadGrid.
While loading I have set AutoPostBack='True' for dropdownlist and also created the event SelectedIndexChanged.
DropDownList ddlConditions = new DropDownList();
ddlConditions.ID = "ddl" + name;
ddlConditions.AutoPostBack = true;
ddlConditions.SelectedIndexChanged += new EventHandler(ddlConditions_SelectedIndexChanged);
My question is while i change the selected index of dropdownlist the event SelectedIndexChanged is not getting triggered.
Can anyone help me to solve this problem?
Thanks in advance.
Usually this caused by a page life cycle problem. When your index changed event of Dropdownlist fires the control doesn't exist to bind it on the postback.
Example:
-> MyEvent fires.
-> Drop-down created.
-> Event Handler specified.
-> Index Changed event triggered. Page reloads. Drop-down not found, cannot fire.
So you have to ensure that the drop-down is created before .NET attempts to handle the event.
Please refer this answer for more information regarding this type of issue and life cycle.
I can suggest you to check the place where you have created DropDownList. Dynamic controls should be added on OnInit or at least on OnLoad. After OnLoad finishes executing ASP.NET starts processing control's events and values.
My question is while i change the selected index of dropdownlist the
event SelectedIndexChanged is not getting triggered.
Answer: because you have created DropDownList after the events have been processed.
Is the page posting back? If so, you'll need to make sure that the control is recreated on the page on every postback.
If it's inside the usual if(!IsPostBack) block, then put it outside - Usually, it's prudent to create controls in page_init as well, but that can depend on your specific setup.

Not able to fill a DropDownList according to the selection of another DropDownList

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.

ASP.net listbox control return -1

I am trying to put a listbox control on my ASP.net page, and when I click it, the selectedindex value is always -1. Why is it doing that? I set AutoPostBack to true. Why is it always returning -1?
Please let me know.
Thanks
There could be many reasons why but I am guessing that you are loading the contents of the ListBox on every load of the page.
Wrap your data binding code in an if statement like this to allow the control to retain what index you selected:
if (!this.IsPostBack)
{
// data binding code here
}
It does depend on what your doing, but -1 normally means nothing is selected when a PostBack is occurring, or that the list of items is empty that the control is being databound to.

DropDownList doesn't postback on SelectedIndexChanged

I'm writing an ASP.Net webform with some DropDownList controls on it. Then user changes selected item in one of dropdowns, ASP.Net doesn't seem to handle SelectedIndexChanged event until form is submitted with a 'Submit' button click.
How do I make my dropdowns handle SelectedIndexChanged instantly?
P.S. It's a classic question I have answered too many times, but it seems no one asked it before on stackoverflow.
Setting the AutoPostback property to true will cause it to postback when the selection is changed. Please note that this requires javascript to be enabled.
You need to set the AutoPostBack property of the list to true.
Also, if you're populating the contents of the drop down list from the code behind (getting the contents of the list from a database, for example) - make sure you're not re-binding the data in every postback.
Sometimes people are caught out by binding the drop-down in the page load event without putting it in an If Not IsPostBack. This will cause the event not to fire.
The same is also true of repeaters and ItemCommand events.
if you are populating the dropdown list during page load then each time the page postback it will reload the list thus negating your postback method.
you need to be sure to load the dropdownlist only if (!ispostback)
Set the AutoPostBack property of DropDownList to true.

Resources