TimeTrap
TimeTrap

Reputation: 1142

Trouble filtering a GridView using a DropDown Control

I'm trying to filter a gridview using a dropdown, but it appears that the dropdown value that is getting selected isn't making it to my SqlDataSource control. (I'm new to asp.net and this is a tutorial I'm working on.)

When I remove the SelectParameters section and plug one a valid LastName into the where clause of the gridview's select statement, I get a filtered gridview as I would expect, but it doesn't work when I'm selecting a name from the dropdown.

This may be a VS version thing. I'm using VS 2010 (w/.Net 3.5) where the book uses VS 2008.

The code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SqlDataSourceParameters_3.aspx.cs" Inherits="SqlDataSourceWizard" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    <asp:SqlDataSource ID="CustomersDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" 

        SelectCommand="SELECT [FirstName], [LastName], [EmailAddress], [ModifiedDate] FROM [Person].[Contact] WHERE ([LastName] = @LastName)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" 
                Name="LastName" PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="CustomerGridView" runat="server" AutoGenerateColumns="False" 
        DataSourceID="CustomersDataSource" EnableModelValidation="True">
        <Columns>
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" 
                SortExpression="EmailAddress" />
            <asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate" 
                SortExpression="ModifiedDate" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="StaffDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString2 %>" 
        SelectCommand="SELECT DISTINCT [LastName] FROM [Person].[Contact] ORDER BY 1"></asp:SqlDataSource>
    <asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="StaffDataSource" DataTextField="LastName" 
        DataValueField="LastName">
    </asp:DropDownList>
    </form>
</body>
</html>

Upvotes: 0

Views: 466

Answers (1)

Kris Krause
Kris Krause

Reputation: 7326

Maybe I am missing something here but what is causing a postback? I do not see a button nor do I see the dropdown's autopostback property set to true. Is your code behind doing something (like setting the default value of the dropdown)?

Upvotes: 1

Related Questions