0

我正在使用 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 的了解还不够防止这种行为。任何帮助将非常感激。

4

1 回答 1

0

我最终解决了我自己的问题。我创建了一个会话变量来存储页面加载之间的数据视图,并在执行查询之前检查数据视图是否已存储,如果是则对其进行排序,否则仅执行常规查询。由于我不希望在初始页面视图和排序之间引入数据,因此我认为使用存储的数据副本不会是一个主要问题。

于 2010-11-21T23:10:40.747 回答