我有連接到一個SqlDataSource一個GridView,它工作正常,我可以與原始數據正確排序。ASPX代碼隱藏 - 更改的SelectCommand導致排序恢復到原始數據
如果我告訴代碼隱藏找到通過ASPX頁面(SqlDataSource2)上預先定義SqlDataSource的具體信息,隨後的分揀工作正常。
如果我告訴代碼隱藏修改預先存在的SqlDataSource和綁定的是,後續的分揀工作不正常,但返回原始數據。
上一篇: SQL在Web.config中定義的數據源連接字符串,其中包含表document_index中的id,last_modified和view_count列。
ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT id, last_modified, view_count FROM [document_index] ORDER by id"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
SelectCommand= "SELECT id, last_modified, view_count FROM [document_index] WHERE view_count LIKE '7'"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button runat="server" Text="Select View Using ASPX SQL Source2 (Sort Works)" OnClick="aspxviewchange" />
<asp:Button runat="server" Text="Select View Using Codebehind SQL Source1 Change Data (Sort Breaks)" OnClick="cbviewchange" />
<asp:GridView ID="GridView1" EnableSortingAndPagingCallbacks= "true" runat="server" AllowSorting="True" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" Width="100%" CellPadding="4" Padding="20" DataKeyNames="id" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="25" AllowCustomPaging="True">
<Columns>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
C#代碼隱藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CreateNewColumn("last_modified", "Last Modified");
CreateNewColumn("view_count", "Views");
}
}
protected void CreateNewColumn(string SQLcolname, string header)
{
BoundField NewColumnName = new BoundField();
NewColumnName.DataField = SQLcolname;
NewColumnName.SortExpression = SQLcolname;
NewColumnName.HeaderText = header;
GridView1.Columns.Add(NewColumnName);
}
protected void aspxviewchange(object sender, EventArgs e) // SUBSEQUENT SORTING WORKS
{
GridView1.DataSourceID = "SQLDataSource2";
GridView1.DataBind();
}
protected void cbviewchange(object sender, EventArgs e) // SUBSEQUENT SORTING BROKEN
{
SqlDataSource1.SelectCommand = "SELECT id, last_modified, view_count FROM [document_index] WHERE view_count LIKE '7'";
GridView1.DataSourceID = "SQLDataSource1";
GridView1.DataBind();
}
}
選擇ASPX SQL按鈕排序正確,以及隨後的分揀工作。 選擇Codebehind SQL按鈕排序正確,但後續排序不起作用。
您尚未發佈足夠的代碼。基本上它取決於這個代碼在頁面事件生命週期中執行的位置,這是陳舊和難以解釋的。如果可能的話,我會推薦將您的整個代碼放在後面。 –
對不起,我編輯了我的原始文章併發布了完整的代碼。 –