C# SqlDataSource通配符是否可分页
我使用的是Visual Studio 2010,C#,带有C# SqlDataSource通配符是否可分页,c#,sql,C#,Sql,我使用的是Visual Studio 2010,C#,带有SqlDataSource。我有一个不同类型的食谱数据库。我在使用带有通配符的WHERE子句时遇到了一个问题 我可以查询数据库并在可分页和可排序的GridView或ListView中显示食谱,如果我使用标准查询,如 SELECT * From PostedRecipes 当我使用通配符查询(如 SELECT RecipeName From PostedRecipes WHERE RecipeName LIKE '%' + @Rec
SqlDataSource
。我有一个不同类型的食谱数据库。我在使用带有通配符的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页可用。如果单击第二或第三页,则该页为空白。如果单击“排序”,页面将返回空白
使用VisualStudio,我不需要为返回上述结果添加任何代码
我找不到关于这个特定主题的任何文档或文献,关于是否有可能完成我正在尝试做的事情,也就是说,如上所述,使用通配符创建一个可分页和可排序的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>
这是一个可排序和分页的页面,但不包含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>
带有WHERE子句的页面代码。此页面呈现搜索的术语recipes,但不会对-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>
你的问题措辞很不幸——没有理由认为一个重要的查询是不可移植的,更不用说不可分页的了。(您可以获取所有行并在客户端上执行此操作,这非常简单,但效率低下。或者您可以自动更改查询以添加分页/排序子句-无论SQL Server等效于
LIMIT..OFFSET
是什么,以及ORDER BY
)给出一些代码示例,您正在使用这些代码将GridView
连接到一个非平凡的查询,并询问如何使其在客户端或服务器上可分页+可排序。至于“不需要添加任何代码隐藏”的话,可能有必要回答您的问题-通过散文分享GUI设计器界面的状态不是很简单,因此为了您的问题,您应该尽可能少地在那里做一些实际的事情。millimoose,请参阅我上面的代码编辑。为什么您的第二个查询有分组依据
和排序依据
?我不确定这些是否会改变什么,但第一个似乎没有必要,第二个可能会干扰ASP.NETAlso所做的任何排序,横向建议:使用和LINQ到实体或LINQ到SQL。它被吹捧为使用数据源控件的首选选项。(首先,它不那么难以捉摸,分页和排序适用于返回IQueryable
的任何内容)
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)
{
}
}
<%@ 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>
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)
{
}
}