好的努力让我的头脑跟随下面的例子。 (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.filterexpression.aspx)
目标:有两个下拉列表/文本框,用于编辑数据网格过滤。
我尝试在= title之后添加FirstName ='{1}'以及FirstName ='{0}'“,但这似乎不起作用。我为FirstName添加了一个控制参数。
以下代码首先是我的版本,然后是原版。
我的代码
<!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>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<p>Show all employees with the following title:
<asp:DropDownList
id="DropDownList1"
runat="server"
AutoPostBack="True">
<asp:ListItem Selected="True">A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList></p>
<asp:DropDownList
id="DropDownList2"
runat="server"
AutoPostBack="True">
<asp:ListItem Selected="True">X</asp:ListItem>
<asp:ListItem>Y</asp:ListItem>
<asp:ListItem>Z</asp:ListItem>
</asp:DropDownList></p>
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
FilterExpression="Title='{0}' or "FirstName='{1}'">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
<asp:ControlParameter Name="FirstName" ControlId="DropDownList1" PropertyName="SelectedValue"/>
</FilterParameters>
</asp:SqlDataSource>
<p><asp:GridView
id="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<columns>
<asp:BoundField Visible="False" DataField="EmployeeID" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
</columns>
</asp:GridView></p>
</form>
</body>
</html>
答案 0 :(得分:0)
我最初用逗号分隔搜索查询。我实际上需要把'或'。
查看原始帖子的完整代码(编辑为正确)和下面的示例代码..
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees"
FilterExpression="Title='{0}' or "FirstName='{1}'">