• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

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.

The Two Bound DDLs:
 
<table width="100%" cellspacing="0" cellpadding="8">
                                    <tr valign="middle" align="left" style="background-color: aliceblue;">
                                    <td>Client No:</td>
                                <td><asp:DropDownList ID="DDLClientNo" DataSourceID="ClientNo" runat="server" OnSelectedIndexChanged="DDLClientNo_SelectedIndexChanged" 
                                    AutoPostBack="true" DataTextField="client_ref" DataValueField="client_ref" AppendDataBoundItems="true">
                                    <asp:ListItem Text="n/a" Value="0" />
                                    </asp:DropDownList>

                                    <asp:SqlDataSource ID="ClientNo" ConnectionString="<%$ connectionStrings:xxxxxxxxx %>"
        SelectCommand="Select DISTINCT client_ref from matter order by client_ref asc" runat="server"></asp:SqlDataSource>
                                </td>
                                 <td>Matter Name:</td>
                                        <td><asp:DropDownList ID="DDLMatterName" DataSourceID="MatterName" runat="server" 
                                            DataTextField="matter_suffix" DataValueField="matter_suffix" AppendDataBoundItems="true" >
                                            <asp:ListItem Text="n/a" Value="0" />
                                            </asp:DropDownList>
                                            <asp:SqlDataSource ID="MatterName" ConnectionString="<%$ connectionStrings:xxxxxxx %>"
        SelectCommand="Select DISTINCT 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></tr></table>

Open in new window

The Code Behind:
 protected void DDLClientNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddList = sender as DropDownList;
        Session["client_ref"] = ddList.SelectedValue.ToString();
    }

Open in new window

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...

many thanks in adv.
0
forsters
Asked:
forsters
  • 12
  • 11
3 Solutions
 
Bob LearnedCommented:
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...
0
 
Bob LearnedCommented:
Do the two N/A values have the same "value" attribute?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
forstersAuthor Commented:
I've set both to 0
0
 
Bob LearnedCommented:
The values need to be unique.
0
 
forstersAuthor Commented:
It appears to make no difference.
0
 
Bob LearnedCommented:
Is this how you are filtering?

                                      <asp:SqlDataSource ID="MatterName" ConnectionString="<%$ connectionStrings:xxxxxxx %>"
        SelectCommand="Select DISTINCT 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>
 

Open in new window

0
 
forstersAuthor Commented:
yes - I have actually removed the 'DISTINCT' from that statement as it's not necessary.
0
 
Bob LearnedCommented:
I see where you set the Session parameter here:

Session["client_ref"] = ddList.SelectedValue.ToString();

Open in new window


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.
0
 
Bob LearnedCommented:
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.
0
 
Bob LearnedCommented:
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>

Open in new window

0
 
Bob LearnedCommented:
What is the difference when adding the N/A items?

      <asp:ListItem Text="n/a" Value="0" />

Do the data-bound items have value > 0?
0
 
forstersAuthor Commented:
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.
0
 
Bob LearnedCommented:
"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?

Session["client_ref"] = ddList.SelectedValue.ToString();

Open in new window

0
 
forstersAuthor Commented:
that depends what the user selects from the first dropdown
0
 
Bob LearnedCommented:
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 :

http://weblogs.asp.net/scottgu/436804

thanks for your help, I will update if I get the workaround to play nicely..
0
 
Bob LearnedCommented:
You might try to add the items in the SqlDataSource.Selecting event handler, and not using AppendDataboundItems.

Example:

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        SqlDataSource1.SelectParameters["OwnerID"].DefaultValue = Membership.GetUser(true).ProviderUserKey.ToString();
    }

Open in new window

0
 
forstersAuthor Commented:
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.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 12
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now