我很难让gridview响应排序。通过这个论坛,我在http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.sortparametername.aspx处遇到了一个MSDN示例。我复制并构建了代码,gridview显示正确,但在单击列标题对数据进行排序时没有响应。
由于某些原因,系统没有响应列标题点击,但对我的页面上的其他Web控件(如超链接和按钮)进行了正确响应。我想知道是否有需要配置的Visual Studio 2008设置,或者是否因为我在本地主机上运行它。似乎没有合理性,但我的想法已经用完了。
由于
<%--<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
--%><%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server"
DataSourceID="ObjectDataSource1"
AllowSorting="True">
</asp:GridView>
<asp:ObjectDataSource
ID="ObjectDataSource1"
runat="server"
SelectMethod="SelectMethod"
TypeName="Samples.AspNet.CS.SortingData"
SortParameterName="sortExpression">
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Samples.AspNet.CS
{
public class SortingData
{
public SortingData(){}
private static DataTable table;
private DataTable CreateData()
{
table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Number", typeof(int));
table.Rows.Add(new object[] { "one", 1 });
table.Rows.Add(new object[] { "two", 2 });
table.Rows.Add(new object[] { "three", 3 });
table.Rows.Add(new object[] { "four", 4 });
return table;
}
public DataView SelectMethod(string sortExpression)
{
if (table == null)
{
table = CreateData();
}
DataView dv = new DataView(table);
dv.Sort = sortExpression;
return dv;
}
}
}