0

我正在使用 Visual Studio 2010、C# 和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 个页面可用。如果单击第 2 或第 3 页,则该页为空白。如果单击“排序”,则页面返回空白。

使用 Visual Studio,我无需在后面添加任何代码即可返回上述结果。

我找不到关于这个特定主题的任何文档或文献,关于是否可以做我想做的事情——也就是说,有一个可分页和可排序的GridViewListView使用通配符,如上所述。的属性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)
 {

}
}
4

0 回答 0