Link to home
Start Free TrialLog in
Avatar of forsters
forstersFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I don't understand your question.  Does the DropDownList function correctly, until a value is selected that does not exist in the data source?
Avatar of forsters

ASKER

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?
I've set both to 0
The values need to be unique.
It appears to make no difference.
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

yes - I have actually removed the 'DISTINCT' from that statement as it's not necessary.
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?
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.
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?
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

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

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

Open in new window

that depends what the user selects from the first dropdown
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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