[英]ASP. NET C# GridView Paging
Hi good day to all. 大家好。 I have this web application for a certain company.
我有某个公司的此Web应用程序。 In this web application there is a part that uses GridView to display records from the database and the way it is being displayed it is hard coded.
在此Web应用程序中,有一部分使用GridView来显示数据库中的记录,并且对其显示方式进行了硬编码。 I'll display my codes bellow.
我将在下面显示我的代码。
string SQLCommand = "SELECT LastName +', ' +FirstName + ' '+MiddleInitial AS 'Name', UserName + ' 'As 'User Name', StreetAddress FROM CustomersMaster Where LastName Like '%"+ SearchText.Text.Trim() + "%'";
SqlCommand com = new SqlCommand(SQLCommand, con);
SqlDataAdapter adp = new SqlDataAdapter(com);
DataTable tbl = new DataTable();
adp.Fill(tbl);
AdminViewBuyersGV.DataSource = tbl;
AdminViewBuyersGV.DataBind();
My problem is I want to use the "Paging" property of the GridView but when I activated the "Paging" property and then when I run it there's an error that says that "The data source does not support server-side data paging". 我的问题是我想使用GridView的“ Paging”属性,但是当我激活“ Paging”属性然后运行它时,出现一个错误,提示“数据源不支持服务器端数据分页”。 I just want to know how to use paging when I already hard-coded it.
我只想知道已经对它进行硬编码时如何使用分页。
Is there a way on how to solve my problem. 有没有办法解决我的问题。 Thank You in Advance and God Bless!
在此先感谢您,上帝保佑! :)
:)
Make sure you are acting on data... 确保您正在处理数据...
if (tbl.Rows.Count > 0)
{
AdminViewBuyersGV.DataSource = tbl;
AdminViewBuyersGV.DataBind();
}
else
{
// no records
}
It says it doesn't like the data you are binding. 它说它不喜欢您要绑定的数据。 So try to tidy the data you are binding:
因此,请尝试整理要绑定的数据:
AdminViewBuyersGV.DataSource = tbl.ToList();
您是否为AdminViewBuyersGV_PageIndexChanging
例程编写代码?
Unless you have a good reason for binding your datasource in code, you might want to look at using a SqlDataSource control in the .aspx page. 除非有充分的理由将数据源绑定到代码中,否则您可能需要查看在.aspx页中使用SqlDataSource控件。 You can give it a parameter (which is safer than building a sql string the way you are) and it should support paging out of the box...
您可以给它提供一个参数(比您以自己的方式构建sql字符串更安全),它应该支持开箱即用的分页...
Ok, so full example using SqlDataSource (as Kendrick mentioned): 好的,使用SqlDataSource的完整示例(如Kendrick所述):
First you need to collect the parameters from your query, which it looks like you are already doing in a textbox somewhere. 首先,您需要从查询中收集参数,好像您已经在某个地方的文本框中一样。 Something like this:
像这样:
<asp:Label ID="lblLastName" runat="server" AssociatedControlID="tbLastName" Text="Last Name" ClientIDMode="Static" />
<asp:TextBox ID="tbLastName" runat="server" ClientIDMode="Static" />
<asp:Button ID="btnSearch" runat="server" ClientIDMode="Static" Text="Search" />
Now we need to take your Raw Inline SQL out of the code behind and move it into a SqlDataSource. 现在,我们需要从原始代码中删除Raw Inline SQL,并将其移到SqlDataSource中。 We also want to get rid of the potential SQL Injection you have in your query by using a parameterized query instead :) In order to do that we need to hook up our TextBox to the parameter, but luckily DataSource controls allow us to do this without any code by using SelectParameters .
我们还希望通过使用参数化查询来摆脱查询中可能存在的SQL注入 :)为此,我们需要将TextBox连接到该参数,但是幸运的是,DataSource控件允许我们执行以下操作:使用SelectParameters的任何代码。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ContactManagerConnectionString %>"
SelectCommand="SELECT LastName + ', ' + FirstName + ' ' + MiddleInitial AS 'Name', UserName AS 'User Name', StreetAddress FROM CustomersMaster WHERE (LastName LIKE '%' + @SearchText + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="tbLastName" ConvertEmptyStringToNull="true"
Name="SearchText" PropertyName="Text" DbType="String" />
</SelectParameters>
</asp:SqlDataSource>
Once those two pieces are in place it is just a matter of setting up your grid the way you want, and setting the AllowPaging property to true. 一旦这两部分就位,只需按所需方式设置网格,并将AllowPaging属性设置为true。
<asp:GridView runat="server" AllowPaging="True" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True"
SortExpression="Name" />
<asp:BoundField DataField="User Name" HeaderText="User Name"
SortExpression="User Name" />
<asp:BoundField DataField="StreetAddress" HeaderText="StreetAddress"
SortExpression="StreetAddress" />
</Columns>
</asp:GridView>
Codeless paging* 无代码分页*
* It is worth noting that this is pretty poor in terms of optimization as all the paging work is done on the Web Server. *值得注意的是,这在优化方面非常差,因为所有分页工作都是在Web服务器上完成的。 To really make this efficient you would want to employ some paging at the database level.
为了真正提高效率,您需要在数据库级别使用一些分页。 Otherwise you will bring back every record every single time.
否则,您将每次带回每条记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.