Query ASP.NET Datalist with Drop Down Menu.

Posted on 2012-08-18
Last Modified: 2012-08-19
I have a menu on my Masterppage and on one of the links I want to retrn the page im seeking (food-drinks.aspx) and at the same time query and return the results in the DataList in food-drinks.aspx.  I want to query the  value "type" in the SqlDatasource.

Thanks  for any assistance.


                    <ul id="menu">
                    <ul id="nav-one" class="dropmenu css-only">

                  <li><a href="#">Tutorials</a></li>
                        <a href="#">Real Estate</a>
                              <li><a href="#">Rental</a></li>
                              <li><a href="#">Purchase</a></li>                               
                  <li><a href="#">place</a></li>


<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT [id],  [type], [image], [title] FROM [recipe] WHERE ([type] = @type)">
            <asp:ControlParameter ControlID="ListBox1" DefaultValue="type" Name="type"
                PropertyName="SelectedValue" Type="String" />
    <asp:DataList ID="DataList1" runat="server" DataKeyField="id"
        DataSourceID="SqlDataSource1" CellPadding="10" Font-Bold="False"
        Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
        Font-Underline="False" HorizontalAlign="Right" RepeatColumns="5">
        <HeaderStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
            Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Justify" />
        <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
            Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Justify" />
            &nbsp;<asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>' />
            <br />
            <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("image") %>'
                Width="140px" />
            <br />
            <asp:HyperLink ID="HyperLink1" runat="server"
                NavigateUrl='<%# String.Format("Recipe.aspx?title={0}", Eval ("title")) %>'
                Text='<%# Bind ("title") %>'> </asp:HyperLink>
            <br />
            <br />
Question by:misnstt
    LVL 26

    Accepted Solution

    Hi misnstt,
    probably have more control using a stored procedure.
    CREATE PROCEDURE [dbo].[Recipe_Sel]
    	@Type varchar(50) = NULL
        IF ISNULL(@Type,'') = '' BEGIN
            -- return all records, should never happen if input param is bount to a control param
        	SELECT [id],  [type], [image], [title] 
    		FROM [recipe] 
        ELSE BEGIN
            -- return records matching @Type input param
    		SELECT [id],  [type], [image], [title] 
    		FROM [recipe] 
    		WHERE [type] = @type
    -- Testing
    -- Return all recipes
    -- exec Recipe_Sel 
    -- Return Curry recipes
    -- exec Recipe_Sel 'Curry'  

    Open in new window

    Not sure you need the DefaultValue="type", unless you have a recipe [Type] called 'type'.
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  SelectCommand="Recipe_Sel" SelectCommandType="StoredProcedure" >
                <asp:ControlParameter ControlID="ListBox1" Name="type"  PropertyName="SelectedValue" Type="String" />

    Open in new window

    Alan ";0)

    Author Closing Comment

    Thanks for your fast assistance.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    The viewer will learn how to dynamically set the form action using jQuery.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now