我正在使用带有SqlDataSource
的Visual Studio 2010 C#。我有一个不同类型食谱的数据库。当我使用带有通配符的WHERE
子句时,我遇到了一个问题。
如果我使用像
这样的标准查询,我可以查询数据库并在可分页和可排序的GridView
或ListView
中显示食谱
SELECT * From PostedRecipes
当我使用通配符查询(例如
)时会出现问题SELECT RecipeName
From PostedRecipes
WHERE RecipeName LIKE '%' + @RecipeName + '%'
GROUP BY RecipeName
例如,如果数据库有100个含有30个三明治食谱的食谱,并且我在“沙子”上搜索,那么将显示30个三明治食谱。如果页面大小设置为30,并且有30个三明治配方,那么将显示30个配方,但不可排序。
问题不在于让WHERE
使用通配符,而是让结果可分页和排序。
如果页面大小更改为10,则会显示10个三明治配方,并指示还有2个页面可用。如果单击第2页或第3页,则页面为空白。如果单击“排序”,页面将返回空白。
使用Visual Studio,我不需要添加任何代码来返回上述结果。
我找不到关于这个特定主题的任何文档或文献,关于是否可以做我想做的事情 - 也就是说,有一个可分页和可排序的GridView
或ListView
使用一张通配符,如上所述。 SqlDataSource
的属性设置为DataSet
并启用缓存。任何正确方向的帮助都将受到赞赏。
修改
这是源页面 - Search.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="RecipeFaire.Search" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="RecipeName"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search"
PostBackUrl="~/GridTest3.aspx" />
</div>
</form>
</body>
</html>
搜索.cs
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace RecipeFaire
{
public partial class Search : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
这是一个可排序且可分页的页面,但不包含WHERE子句。它呈现数据库中的所有配方 - GridTest2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridTest2.aspx.cs" Inherits="GridTest2" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RecipeUploadConnectionString %>"
SelectCommand="SELECT [RecipeID], [RecipeName], [Description] FROM [PostedRecipes]">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="RecipeID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="RecipeID" HeaderText="RecipeID" InsertVisible="False"
ReadOnly="True" SortExpression="RecipeID"></asp:BoundField>
<asp:BoundField DataField="RecipeName" HeaderText="RecipeName" SortExpression="RecipeName">
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description">
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
由Visual Studio生成的GridTest2代码 - GridView.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridTest2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
带有WHERE子句的页面代码。此页面呈现搜索的术语配方,但不会页面或排序 - GridTest3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridTest3.aspx.cs" Inherits="GridTest" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!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>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:RecipeUploadConnectionString %>"
SelectCommand="SELECT [RecipeID], [RecipeName], [Description] FROM [PostedRecipes] WHERE ([RecipeName] LIKE '%' + @RecipeName + '%')
GROUP BY RecipeID, RecipeName, Description ORDER BY RecipeID, RecipeName, Description">
<SelectParameters>
<asp:FormParameter FormField="RecipeName" Name="RecipeName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="RecipeName" HeaderText="RecipeName"
SortExpression="RecipeName"></asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Visual Studio生成的GridTest3.aspx代码 - GridTest3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class GridTest3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}