使用存储过程的Gridview分页
本文关键字:Gridview 分页 存储过程 | 更新日期: 2023-09-27 18:10:26
嗨,我有一个问题,得到我的gridview分页。我已经读了一堆关于传递页码和每页记录到我的存储过程的文章,但问题是我的存储过程是完全动态的,使用一些临时表彼此连接,所以我已经尝试了什么是建议。我只是不知道还能怎么做。我不应该为SQLDataSource使用默认分页吗?
<asp:GridView ID="gvFileResults" DataSourceID="SqlDataSource2" runat="server" AutoGenerateColumns="False" EmptyDataText="Nothing Found" AllowSorting="True" AllowPaging="True" PageSize="50" OnPageIndexChanging="gvFileResults_PageIndexChanging">
<Columns>
<asp:HyperLinkField DataTextField="DisplayName" HeaderText="File Name" DataNavigateUrlFields="Drawer_Id, ParentFolders_Id, FolderId, Id" Target="_blank" SortExpression="DisplayName" DataNavigateUrlFormatString="~/Cabinet/FolderView.aspx?did={0}&fid={1}&sid={2}&fileid={3}">
<ControlStyle CssClass="gridlink" />
</asp:HyperLinkField>
<asp:BoundField DataField="FileType" HeaderText="File Type" ItemStyle-Width="100px" SortExpression="FileType" >
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:TemplateField HeaderText="Location" ItemStyle-Width="200px">
<ItemTemplate>
<div class="drawername"><%#Eval("DrawerName")%></div>
<div class="foldername"><%#Eval("ParentFolderName")%></div>
<div class="subfoldername"><%#Eval("FolderName")%></div>
</ItemTemplate>
<ItemStyle Width="200px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
我已经尝试使用存储过程在SQLDataSource从代码后面返回结果,但我不能得到分页工作。我尝试过直接在带有参数的页面上使用SQLDataSource,但我无法返回任何结果
下面是不返回结果的页面上的数据源的代码
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FileCabConnection %>" SelectCommand="spSearchFiles" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="tbFileName" DefaultValue="''" Name="FileName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="tbFileDescription" Name="FileDescription" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="ddlFileType" Name="FileTypeId" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="ddlDrawer" Name="DrawerId" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="ddlFolder" Name="FolderId" PropertyName="SelectedValue" Type="Int32" />
<asp:ControlParameter ControlID="ddlFolder" Name="SubFolderId" PropertyName="SelectedValue" Type="Int32" />
<asp:SessionParameter Name="UserId" SessionField="UserId" Type="Int32" />
<asp:SessionParameter Name="SecurityLevelId" SessionField="SecurityLevelId" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
这里是返回结果的数据源的代码但是分页不起作用
SqlDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings["FileCabConnection"].ConnectionString;
SqlDataSource1.SelectCommand = "spSearchFiles";
SqlDataSource1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
SqlDataSource1.SelectParameters.Add(new Parameter("FileName", DbType.String, tbFileName.Text));
SqlDataSource1.SelectParameters.Add(new Parameter("UserId", DbType.Int32, user.Id.ToString()));
if (tbFileDescription.Text != "")
{
SqlDataSource1.SelectParameters.Add(new Parameter("FileDescription", DbType.String, tbFileDescription.Text));
}
if (Convert.ToInt32(ddlFileType.SelectedValue) > 0)
{
SqlDataSource1.SelectParameters.Add(new Parameter("FileTypeId", DbType.Int32, ddlFileType.SelectedValue));
}
if (Convert.ToInt32(ddlDrawer.SelectedValue) > 0 && Convert.ToInt32(ddlFolder.SelectedValue) == 0 && Convert.ToInt32(ddlSubfolder.SelectedValue) == 0)
{
SqlDataSource1.SelectParameters.Add(new Parameter("DrawerId", DbType.Int32, ddlDrawer.SelectedValue));
}
if (Convert.ToInt32(ddlFolder.SelectedValue) > 0 && Convert.ToInt32(ddlSubfolder.SelectedValue) == 0)
{
SqlDataSource1.SelectParameters.Add(new Parameter("FolderId", DbType.Int32, ddlFolder.SelectedValue));
}
if (Convert.ToInt32(ddlSubfolder.SelectedValue) > 0)
{
SqlDataSource1.SelectParameters.Add(new Parameter("SubFolderId", DbType.Int32, ddlSubfolder.SelectedValue));
}
gvFileResults.DataSource = SqlDataSource1;
gvFileResults.DataBind();
为Grid添加以下两个属性,以启用网格内的分页。AllowPaging = " true "页大小= " 5 "
包含以下两个参数到SQL数据源,它将传递当前行索引和页面大小。
将页面大小和页面索引参数添加到SP中,并将以下代码添加到SP中的select语句末尾:
ORDER BY [ColumnName]OFFSET @PageIndex * @PageSize ROWS