使用 LINQ 数据源进行分页、排序、编辑
好吧,我已经按照 Scott Gu 在他的第 9 部分等博客中的例子,尽管我尽最大努力实现“自动排序、分页和编辑”,但我无法让某某工作。
通过以下设置可以自动排序、分页和编辑吗?
<asp:TextBox ID="tbxHowMany" runat="server"></asp:TextBox>
<asp:RadioButtonList ID="radMaterial" runat="server">
<asp:ListItem>Paper</asp:ListItem>
<asp:ListItem>Glass</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="LQTOPDS" Font-Size="XX-Small" ForeColor="#333333"
GridLines="None" DataKeyNames="PriKey"
AllowPaging="True" AllowSorting="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Productpriority" HeaderText="Productpriority"
SortExpression="Productpriority" />
<asp:BoundField DataField="MemberId" HeaderText="MemberId"
SortExpression="MemberId" />
<asp:BoundField DataField="UnitWeight" HeaderText="UnitWeight"
SortExpression="UnitWeight" />
<asp:BoundField DataField="WeightUnitCode" HeaderText="WeightUnitCode"
SortExpression="WeightUnitCode" />
<asp:BoundField DataField="RecycledContent" HeaderText="RecycledContent"
SortExpression="RecycledContent" />
<asp:BoundField DataField="IsBiodegradable" HeaderText="IsBiodegradable"
SortExpression="IsBiodegradable" />
<asp:BoundField DataField="Recyclability" HeaderText="Recyclability"
SortExpression="Recyclability" />
<asp:BoundField DataField="RevisionSourceCode" HeaderText="RevisionSourceCode"
SortExpression="RevisionSourceCode" />
<asp:BoundField DataField="PriKey" HeaderText="PriKey"
SortExpression="PriKey" ReadOnly="True" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:LinqDataSource ID="LQTOPDS" runat="server"
ContextTypeName="LQTOPDSDataContext" EnableInsert="True" EnableUpdate="True"
TableName="tblOnlineReportingCOMPLETEWEIGHTSFINALs"
Where="MaterialText == @MaterialText && Productpriority <= @Productpriority">
<WhereParameters>
<asp:ControlParameter ControlID="radMaterial" Name="MaterialText"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="tbxHowMany" Name="Productpriority"
PropertyName="Text" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
目前我背后唯一的代码如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSourceID = null;
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSourceID = "LQTOPDS";
GridView1.DataBind();
}
}
这让我发疯......我读到这种排序/分页/编辑功能应该是自动的,但显然我做错了一些事情。
如果有人可以向我解释为什么没有生成此功能,我将不胜感激。
Right, ok, I have followed Scott Gu's example in his part 9, etc, etc blog and despite my best efforts to achieve 'automatic sorting, paging and editing, I cannot get the so and so to work.
Is automatic sorting, paging and editing possible with the following set up?
<asp:TextBox ID="tbxHowMany" runat="server"></asp:TextBox>
<asp:RadioButtonList ID="radMaterial" runat="server">
<asp:ListItem>Paper</asp:ListItem>
<asp:ListItem>Glass</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="LQTOPDS" Font-Size="XX-Small" ForeColor="#333333"
GridLines="None" DataKeyNames="PriKey"
AllowPaging="True" AllowSorting="True">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Productpriority" HeaderText="Productpriority"
SortExpression="Productpriority" />
<asp:BoundField DataField="MemberId" HeaderText="MemberId"
SortExpression="MemberId" />
<asp:BoundField DataField="UnitWeight" HeaderText="UnitWeight"
SortExpression="UnitWeight" />
<asp:BoundField DataField="WeightUnitCode" HeaderText="WeightUnitCode"
SortExpression="WeightUnitCode" />
<asp:BoundField DataField="RecycledContent" HeaderText="RecycledContent"
SortExpression="RecycledContent" />
<asp:BoundField DataField="IsBiodegradable" HeaderText="IsBiodegradable"
SortExpression="IsBiodegradable" />
<asp:BoundField DataField="Recyclability" HeaderText="Recyclability"
SortExpression="Recyclability" />
<asp:BoundField DataField="RevisionSourceCode" HeaderText="RevisionSourceCode"
SortExpression="RevisionSourceCode" />
<asp:BoundField DataField="PriKey" HeaderText="PriKey"
SortExpression="PriKey" ReadOnly="True" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:LinqDataSource ID="LQTOPDS" runat="server"
ContextTypeName="LQTOPDSDataContext" EnableInsert="True" EnableUpdate="True"
TableName="tblOnlineReportingCOMPLETEWEIGHTSFINALs"
Where="MaterialText == @MaterialText && Productpriority <= @Productpriority">
<WhereParameters>
<asp:ControlParameter ControlID="radMaterial" Name="MaterialText"
PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="tbxHowMany" Name="Productpriority"
PropertyName="Text" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
Currently the only code behind I have is as follows:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSourceID = null;
}
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.DataSourceID = "LQTOPDS";
GridView1.DataBind();
}
}
This is driving me nuts...I'm reading that this sorting/paging/editing functionality should be automatic but obviously I am doing something wrong.
If someone can explain to me why this functionality is not being generated, I would be most grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这最终是通过更改创建数据来源表的方法来实现的。
我删除了本质上用作 where 子句的“SELECT TOP”,并使用了不同的方法。我对数据进行了排名并使用了“take”——这有效地选择了预先排序的数据,从而删除了任何导致 gridview 分页、排序等问题的附加选择。
如果有人想查看代码,请告诉我。
This was finally achieved by changing the method of creating the table from whcih the data was sourced.
I removed what was essentially the "SELECT TOP" I was using as a where clause and used a different method. I ranked the data and used 'take' - this effectively selected the data that was preordered and thus removed any additonal selecting that cuased issues with the gridview pging, sorting, etc.
If anyone would like to see the code, just let me know.