Search With a DropDownlist - 2.0

Posted by David Wier on 05/14/10 | Code Samples

This sample shows a non-code way to use a DropDownList for a search. It populates with the Categories table of the Northwind Database, so to use this code, as presented, you'll need to create a connectionString entry in your Web.Config called 'NorthwindConnectionString'. As you will see, by choosing a category in the DropDownlist, the Gridview is then populated using the selected item from the DropDownList.

All the controls you’ll need on the page are a DropDownList, a Gridview and 2 SQLDataSource controls.

<asp:DropDownListID="ddlCategories" runat="server" DataSourceID="dsCategories"
        DataTextField="CategoryName"
        DataValueField="CategoryID" AutoPostBack="True">
    </asp:DropDownList>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"         
        DataKeyNames
="ProductID" DataSourceID="dsProducts">
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                InsertVisible="False"
                ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                SortExpression="ProductName" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dsCategories" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY[CategoryName]">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="dsProducts" runat="server" 
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT [ProductID], [ProductName] FROM [Products] WHERE ([CategoryID] = @CategoryID) ORDER BY [ProductName]">
    <SelectParameters>
        <asp:ControlParameter ControlID="ddlCategories" Name="CategoryID" 
            PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
































0 Comments

COMMENTS

Name:
URL:
Comment:

Comments are disabled for this article.