我正在使用InterBase数据库扩展ASP.NET 2.0应用程序。我的经验是在PHP / MySQL中,因此我对ASP的熟悉程度目前在2周范围内,并且通过检查同事的代码,我的ASP书的前90页和Google来拼凑起来。在应用程序中,我有一个SqlDataSource控件连接到我的数据库并选择我需要的信息。然后将结果复制到DataView中,在其中修改其中一列中的数据,然后将DataView推送到GridView进行输出。我遇到的问题是我现在无法对GridView进行排序。我按照这里的说明:http://forums.asp.net/p/956540/1177923.aspx,但无济于事。
以下是页面代码:
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="Products" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" OnLoad="ProductsDS_Load"
OnSelected="ProductsDS_Selected" DataSourceMode="DataSet">
</asp:SqlDataSource>
<br />
<asp:Label ID="testlabel" runat="server"></asp:Label>
<br />
<asp:Label ID="testlabel2" runat="server"></asp:Label>
<br /><br />
This table lists all 2000+ numbered projects which are at least partially in process.<br />
The Project number link leads to a more detailed view of that project.<br />
<br />
<asp:Label runat="server" ID="numrows"></asp:Label> results returned.
<br />
<asp:GridView ID="ProductsView" runat="server" EnableModelValidation="True"
AutoGenerateColumns="False" CellPadding="4" OnSorting="ProductsView_Sorting"
ForeColor="#333333" GridLines="None" AllowSorting="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:HyperLinkField HeaderText="Project" SortExpression="PROJECT"
DataTextField="PROJECT" Target="subweeklyreport" DataNavigateUrlFields="PROJECT"
DataNavigateUrlFormatString="Products.aspx?p={0}" />
<asp:BoundField Visible="false" DataField="PROJECTID" />
<asp:BoundField DataField="PART" HeaderText="Part #"
SortExpression="PART" />
<asp:BoundField DataField="DESCRIPTION" HeaderText="Description"
SortExpression="DESCRIPTION" />
<asp:BoundField DataField="ENGMGR" HeaderText="Eng. Mgr."
SortExpression="ENGMGR" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div>
</form>
这是背后的代码:
protected void ProductsDS_Load(object sender, EventArgs e)
{
string SQLQuery = Query would go here;
testlabel2.Text = SQLQuery;
Products.SelectCommand = SQLQuery;
Products.DataBind();
DataView dv = (DataView)Products.Select(new DataSourceSelectArguments());
foreach (DataRow dr in dv.Table.Rows)
{
string name = dr["ENGMGR"].ToString();
string[] explode = name.Split(' ');
string newname;
if (explode.Length == 3)
{
newname = explode[2] + ", " + explode[0];
}
else
{
newname = explode[1] + ", " + explode[0];
}
dr["ENGMGR"] = newname;
//testlabel.Text = dr["ENGMGR"].ToString();
}
Products.DataBind();
//ProductsView.DataSourceID = "Products";
ProductsView.DataSource = dv;
ProductsView.DataBind();
ProductsView.Enabled = true;
ProductsView.Visible = true;
}
protected void ProductsDS_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
numrows.Text = e.AffectedRows.ToString();
}
protected void ProductsView_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = ProductsView.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
ProductsView.DataSource = dataView;
ProductsView.DataBind();
}
}
private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;
switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;
case SortDirection.Descending:
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
我认为发生的事情是,每当GridView对排序进行回发时,它会导致查询再次执行并覆盖对GridView中现有数据进行排序的尝试,但我对ASP不够了解现在来防止这种行为。任何帮助将不胜感激。
答案 0 :(得分:0)
我最终解决了自己的问题。我创建了一个会话变量来存储页面加载之间的数据视图,并在执行查询之前检查数据视图是否存储,如果是,则对其进行排序,否则只执行常规查询。由于我不希望在初始页面视图和排序之间引入数据,因此我认为使用存储的数据副本不会是一个主要问题。