locked
Display Listview Based on dropdown selection RRS feed

  • Question

  • User1356352994 posted

    Hello,

    I appreciate any help with this, I am running in a million directions and I am not sure which is the best way to go.  I have a simple database of basically 2 products, "Widgets" and "Gadgets" are the table names.  They both share some similar attributes, such as "Class".

    I have a drop down list that I performed a UNION SQL clause to combine the 2 products in the same dropdown list.  What I need to do now is to display the appropriate ListView based on the users selection in the dropdown box.  I have AutoPostBack enabled.

    I am not sure how to structure this, if it is possible to not have to do this programmatically that would be awesome.  I have basically been using all the defaults and statically placing the ListViews on the page.

    I wasn't sure if I need to create 2 listviews and setting a visible property to true or false, or if there is a better way to go about this. I am hoping that I can configure the DataSource of the listview based on the selected item of the DropDown menu.

    This was my failed attempt, not sure how relavant it is.

    <asp:DropDownList ID="ProdList" Width="150px" runat="server" 
                DataSourceID="SqlDataSource1" DataTextField="Class" DataValueField="Class" 
                onselectedindexchanged="ProdList_SelectedIndexChanged">
    
            </asp:DropDownList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
                SelectCommand="SELECT DISTINCT Class FROM Widgets UNION ALL SELECT Class FROM Gadgets GROUP BY Class">
            </asp:SqlDataSource>
        </div>
            <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource2">
            </asp:ListView>
            
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
                SelectCommand="SELECT [Title], [ThumbImage], [Price] FROM [Widgets], [Gadgets] WHERE ([Class] = @Class)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ProdList" Name="Class" 
                        PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>



    Tuesday, December 4, 2012 12:56 PM

Answers

  • User-236004499 posted

    Hi,

    I think your query has an issue.

    SELECT [Title], [ThumbImage], [Price] FROM Widgets WHERE Class = @Class
    UNION ALL 
    SELECT [Title], [ThumbImage], [Price] FROM Gadgets WHERE Class = @Class
    

    Add above query on Listview's SQLDatasource.

    I think you are doing for your test purpose. However I would like to point out one is your DB design is incorrect.

    Thanks
    Praitk
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 5, 2012 7:27 PM

All replies

  • User1356352994 posted

    Perhaps make this more simple.. I have a page, I need the page to display either Widgets or Gadgets.  I have tables for both.  I assumed ListView was the best way to do this.  Any suggestions on how to set this up?

    Tuesday, December 4, 2012 4:48 PM
  • User3866881 posted

    <asp:DropDownList ID="ProdList" Width="150px" runat="server"
                DataSourceID="SqlDataSource1" DataTextField="Class" DataValueField="Class"
                onselectedindexchanged="ProdList_SelectedIndexChanged">

    Where's your AutoPostBack?

    If you don't add this, even if you choose something it won't fetch correct things out.

    Please add this:

     

    <asp:DropDownList ID="ProdList" Width="150px" runat="server"
                DataSourceID="SqlDataSource1" DataTextField="Class" DataValueField="Class"
                onselectedindexchanged="ProdList_SelectedIndexChanged" AutoPostBack=True>
    Wednesday, December 5, 2012 1:28 AM
  • User1356352994 posted

    ANYWAY...

    Still need help on this please

    Wednesday, December 5, 2012 8:06 AM
  • User-236004499 posted

    Hi,

    I think your query has an issue.

    SELECT [Title], [ThumbImage], [Price] FROM Widgets WHERE Class = @Class
    UNION ALL 
    SELECT [Title], [ThumbImage], [Price] FROM Gadgets WHERE Class = @Class
    

    Add above query on Listview's SQLDatasource.

    I think you are doing for your test purpose. However I would like to point out one is your DB design is incorrect.

    Thanks
    Praitk
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 5, 2012 7:27 PM