嗨,我遇到了获取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();
答案 0 :(得分:0)
将两个属性添加到Grid,以便在网格中启用分页。 AllowPaging =&#34;真&#34; 每页=&#34; 5&#34;
将两个参数包括在SQL数据源中,它将传递当前行索引和页面大小。
将页面大小和页面索引参数都添加到SP,并将以下代码添加到SP中的结束语句中:
ORDER BY [ColumnName] OFFSET @PageIndex * @PageSize ROWS FETCH NEXT @PageSize ROWS ONLY;