populate dropdownlist from another dropdownlist where each list has AppendDataBoundItems="true"
hi Experts,
I have 2 ddls each bound to a sqldatasource; on the first ddl I have set autopostback to true and I'm using the selected index change event to capture the selected value in a session variable which then filters the data in the second ddl.
As indicated in the code above I want to include an 'n/a' option in each ddl, and if the user selects that value in the first ddl the 'n/a' value should be the only option in the second ddl.
However I now have a situation where the user is potentially going to select a value which doesn't exist in the sqldatasource and in fact whatever value they select from the first ddl, the data in the second ddl no longer filters, is there a way round this?
The alternative would be to add a checkbox to my form ahead of the ddls so they only show if relevant, I'm just curious to see if this can work without...
I don't understand your question. Does the DropDownList function correctly, until a value is selected that does not exist in the data source?
0
forstersAuthor Commented:
It functions correctly until I append the two n/a values; without those it works, with them it doesn't matter what I select in the first ddl, the second one will display all the data, it no longer filters...
Do the two N/A values have the same "value" attribute?
0
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
Is this value what you need it to be to filter correctly?
0
forstersAuthor Commented:
yes that's correct, so that is picking up the selected value from the first ddl and using it to filter the data of the second, it's a one to many relationship in the database table, so each client_ref has a number of matter_suffix.
If it is the value that you expect, then what is the impact of this statement?
"they select from the first ddl, the data in the second ddl no longer filters"
That would suggest that the value in the WHERE clause is incorrect.
0
forstersAuthor Commented:
Well no, the problem only occurs once I append the two additional values (which of course are not within the database) so I am not surprised that at this point the filtering breaks but it has nothing to do with the value in the where clause it is because I am appending onto that an additional value 0. I know this. I don't know if there is a workaround or whether it is simpler to approach differently.
I still don't understand the issue. If the WHERE clause is correct, and the proper values are returned from the database, are you saying that the DropDownList does not show the correct items?
0
forstersAuthor Commented:
The code below filters as expected - that works.
The code I posted in the original question which includes the appended list items does not.
<td>Client No:</td> <td> <asp:DropDownList ID="DDLClientNo" DataSourceID="ClientNo" runat="server" OnSelectedIndexChanged="DDLClientNo_SelectedIndexChanged" AutoPostBack="true" DataTextField="client_ref" DataValueField="client_ref"> </asp:DropDownList> <asp:SqlDataSource ID="ClientNo" ConnectionString="<%$ connectionStrings:xxxxxx%>" SelectCommand="Select DISTINCT client_ref from matter order by client_ref asc" runat="server"></asp:SqlDataSource> </td> <td>Matter No:</td> <td> <asp:DropDownList ID="DDLMatterNo" DataSourceID="MatterNo" runat="server" AutoPostBack="true" DataTextField="matter_suffix" DataValueField="matter_suffix" OnSelectedIndexChanged="DDLMatterNo_SelectedIndexChanged"> </asp:DropDownList> <asp:SqlDataSource ID="MatterNo" ConnectionString="<%$ connectionStrings:xxxxxx %>" SelectCommand="Select matter_suffix from matter where client_ref=@client_ref order by matter_suffix" runat="server"> <SelectParameters> <asp:SessionParameter Name="client_ref" SessionField="client_ref" /> </SelectParameters> </asp:SqlDataSource> </td>
Yes the Databound items are all values >0, the second ddl appears to return a lot more data that without the n/a values, but because I am unfamiliar with the data itself it's quite hard to see what exactly is going on. but whereas prior to the n/a values the second ddl might filter down to just one record - the same selection with the n/a values appended will return 30+ items so it's fairly clear that it isn't filtering as it was.
"the same selection with the n/a values appended will return 30+ items so it's fairly clear that it isn't filtering as it was."
What is the value for this selection here?
Something has to explain why there would be a difference when you add the N/A item, but I haven't seen anything yet, so I can't suggest a different approach, yet...
0
forstersAuthor Commented:
I did a bit of googling on this and it seems this might be a generic problem with trying to filter ddls with appended data - the last few comments in the link below suggest a possible workaround which I will try but it otherwise looks like a non-starter :
I have ended up using the union but it doesn't resolve anything, just a bit neater than appending the data. I will make do with that and a check box, it's messy but will have to do.
Thanks for the input.
0
forstersAuthor Commented:
Essentially the question is unresolved but it seems that what I want to do is not easily done, I have ended up using a work-around
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.