我正在尝试使用下拉列表过滤网格视图,但似乎被选中的下拉列表值没有进入我的 SqlDataSource 控件。(我是 asp.net 的新手,这是我正在编写的教程。)
当我删除 SelectParameters 部分并将一个有效的 LastName 插入到 gridview 的 select 语句的 where 子句中时,我得到了一个过滤的 gridview,正如我所期望的那样,但是当我从下拉列表中选择一个名称时它不起作用。
这可能是VS版本的东西。我正在使用 VS 2010 (w/.Net 3.5),本书使用 VS 2008。
编码:
<%@ 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>