Dependent dropdowns populated by sql database in ASP.NET - asp.net

I have 3 drop-down lists on my page and a data-list. All of them are populated from SQL database based on selected item from previous drop-down.
AutoPostBack is set to true on all three dropdowns, and on first page load they are displayed correctly i.e. on all three dropdowns first item is selected and the result is displayed in data-list based on that selection. But when I change selection in the first drop-down, only second drop-down gets updated (sometimes it updates the third but its inconsistent), and to get the update on the third one I need to change the selection on the second one. The result in data-list mostly remains the same, and updates when I change selection in third drop-down.
What do I need to do to get the data-list updated when I change the selection in first drop down?

Withoug looking at your code it is difficult to answer the question. But by looking at the description of your question, it looks like you are setting AutoPostBack=true, but not handling the OnSelectedIndexChanged event.
First of all you should add OnSelectedIndexChanged event for all the three dropdowns like below.
protected void dropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
In each selectedindex changed event, you need to bind the next dropdown lists.
May be you can have one common function for binding the list view, which you need to call in each SelectedIndexChanged event.
If you just set AutoPostBack=true and not handling the selectedindex changed, then it does not work properly as expected

To get the data-list updated when the first drop-down is changed, you must fire the post-back event manually(for the second and 3rd drop-downs)!
Assuming the data on the 3rd drop-down is based on the selection from the 2nd,on the client Page in JavaScript : Try to have the first item or a default item selected. This would populate the 3rd list.
A good practice would be to have a default element, that is not specific!
It is also a matter of design. If its a typical list where the second and 3rd lists would be changed often locally, then it is best to have it pre-fetched and stored in a local Json object!

to get the list populated on the selection_index changed of the first dropdown change. Call the populating method of the other two dropdown lists...
and on the selection_change event of the second dropdown call the populating method of the third dropdown

Check your selectedIndexchanged method and whenever you are binding data to datalist before that clear the datalist items and then do binding. so you could have clear idea which data is not populating. and post your code here so it wolud give a clear idea

Related

How to generate a dropdown list from another?

I want to generate a drop down list from another drop down list. That is I have a dropdown of countries. When selecting a country,another dropdown must come with values as states of that specific country. How to do that in asp.net using c#?
for each country you have, add a new list item to the drop down list, with text the country and the value some id of the country. On the second drop down list, set the auto post back property to true and add an event to the on selected item change. In the event code, get the selected item and by the second ddl.
Try it!
Tip: add a hidden field on the page, and on the selected item changed event from the first ddl, set the value of the hidden field, the selected value. On the page_load event, verify if the value is string.empty and if is an id in the value. If it is, bind the second ddl.
The technology you're looking for is called a Cascading Dropdown.
If you are using WebForms then Ajax Control Toolkit then this has one built in:
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
Otherwise you might need to do a further search if you are using MVC.
PS I'm not particularly proud of posting a link to the Ajax Control Toolkit as its not the best library out there but it is an easy drop in for what you want. If you're serious about doing a good job then I'd search for better options for cascading dropdowns.

How to control strange behavior of Ext.NET ComboBox controls?

I have few Ext.NET ComboBox controls on a Web Form. Selecting an item from the first fires the ValueChanged event to populate the second, and so on.
Except Force Selection property, I have not altered other properties of the ComboBox control.
I am experiencing odd behavior of Combo Box controls. As all controls get filled via AJAX request, I find it difficult to set focus on any control. As soon as I bring focus on any control, the cursor disappears after it gets filled.
Secondly, one of the ComboBox is not permitting me to select an item from the list. Even if I try to select an item, it automatically brings back the default item back, which is actually a sixth item in the list.
I double checked the queries and there is no way through which sixth item should get selected.
If I try to open the DropDown list using mouse, it opens for few seconds and collapses automatically.
Is there any way to fix these strange issue? Any other third-party open-source control?
I guess that combos are rebinded in each ajax requests
I suggest to rebind combos in Select direct event handler
Also see the following sample
http://examples1.ext.net/#/Form/ComboBox/Ajax_Linked_Combos/

ASP.NET dropdown selected value?

I've an ASP.NET dropdown list and getting selected value by using the .SelectedValue. I also set this value in some cases by assigning to it. I seem to be randomly not getting this value set even when I choose the item from the drop down. Sometimes it works and sometimes it doesn't.
Ideas?
I you are binding your data at Page_Load make sure you're not re-binding by checking if page is post back.
if(!Page.IsPostBack){ /*bind data here*/ }
Make sure you are not re-binding the data on postback before you are able to check the selection.
Also, are you creating listitems and inserting the listitem into multiple lists? If you select one list, it will change the other.

ASP.Net Radio Button List Problem

im having some trouble with the radio button list in ASP.net, for some reason it wont let me select the 2nd item in the radio button list ( index of 1) when i select this item the selected item goes back to 0 (the first item). when i debug the code to see what the selected item is, it shows 0 for some reason, even though i press the 2nd item ( the index should be 1)
can any one think of what i might be doing wrong here ??
on the web form side i have this
<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged" AutoPostBack="true">
</asp:RadioButtonList>
then on the script side, the radio button list is populated by going through an array like so
for (i = 0; i < answersJArray[i].Length; i++)
{
RadioButtonList1.Items.Add(answersJArray[i].ToString());
}
It would be good if you could show us a little of your codebehind or describe your process and events a little more thoroughly. I'd say offhand that you must be databinding the selection of the radiobuttonlist or initializing its selection by some criteria on page load.
If either of these are correct, you'll want to insert a !IsPostBack conditional to make sure you aren't overriding any potential events by rebinding the control inappropriately or reassigning its selected option programmatically.
Yes, you're re-binding the data before you check it (thus the selection changes).
At what time in the page lifecycle are you checking the value? And also, when you do perform databinding?
It's best if you can bind in the markup (via some sort of datasource, I typically use an ObjectDataSource, but whatever is fine).
If you don't do this, you need to prevent databinding when the page is in "PostBack" mode, otherwise it'll be too late to check the value.
I had the same problem.
I discovered that it is because the value of the listItem (the same value of two distinct listItems).
The listItem is constructed by text and value :
The text is used to display in the page.
The value is used in for
other stuff.
If you add two ListItems with the same value, you will get that behaviour: When click in an item, the first one is selected.

Why do I need to bind GridView on every postback to make click events from columns to work

Ok, i have a gridview and on page load, i dynamically add a column to it that contains a link control. this link control has a onclick event associated with it so that when its clicked, i want to do some processing. Now I have noticed that if I just bind the grid the first time (i.e. if(!IsPostBack) and have enableviewstate for the grid to be true, clicking the link in that column, doesnt trigger the onclick event. But if I bind the grid on every postback, everything seems to be working..does anyone know the reasoning behind it?
It happens because you're dynamically adding the column, so on every postback the column has to be created. What you may want to do is to look at Creating a Custom Column.
May be creating the row Id solves your problem. Check out this link and the post marked as answer.
http://forums.asp.net/p/1471128/3408069.aspx#3408069
Regards
Vinay

Resources