排序选项,我错过了什么?

时间:2014-01-03 01:49:46

标签: c# asp.net .net sorting iis

排序工作正常,如果我在Windows中使用本地IIS,但不能在线工作..请帮忙! 这是代码:

    AllowPaging="false" AllowSorting=true
    ShowHeaderWhenEmpty="True"
    onrowdeleting="gridvwAssessments_RowDeleting"
    onselectedindexchanged="gridvwAssessments_SelectedIndexChanged" 
    onpageindexchanging="gridvwAssessments_PageIndexChanging" 
OnRowEditing="EditAssessment" OnRowUpdating="UpdateAssessment" OnRowCancelingEdit="CancelEdit" 
    onsorting="gridvwAssessments_Sorting" >
    <Columns>
    <asp:TemplateField ItemStyle-Width="100px" HeaderText="NAME" SortExpression="Res_Name">
            <ItemTemplate>
                <asp:Label ID="lblFirstName" runat="server" Text='<%# Eval("Res_Name")%>'></asp:Label>
                <asp:HiddenField ID="assessmentId" runat="server" Value='<%# Eval("Id")%>' />
            </ItemTemplate>
     </asp:TemplateField>

CSS:

protected void gridvwAssessments_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dt = ((DataTable)((GridView)sender).DataSource);
        Image sortImage = new Image();
        if (dt != null)
        {
            //Sort the data.
            if (Session["Assessment_SortDir"] != null)
            {
                if (Session["Assessment_SortDir"].ToString() == "ASC")
                {
                    Session["Assessment_SortDir"] = "DESC";
                    sortImage.ImageUrl = "Images\\Desc.gif";
                }
                else
                {
                    Session["Assessment_SortDir"] = "ASC";
                    sortImage.ImageUrl = "Images\\Asc.gif";
                }
            }
            else
            {
                Session["Assessment_SortDir"] = "ASC";
                sortImage.ImageUrl = "Images\\Asc.gif";
            }

            dt.DefaultView.Sort = e.SortExpression + " " + Session["Assessment_SortDir"].ToString();
            gridvwAssessments.DataSource = dt;
            gridvwAssessments.DataBind();

            int columnIndex = 0;
            foreach (DataControlFieldHeaderCell headerCell in gridvwAssessments.HeaderRow.Cells)
            {
                if (headerCell.ContainingField.SortExpression == e.SortExpression)
                {
                    columnIndex = gridvwAssessments.HeaderRow.Cells.GetCellIndex(headerCell);
                }
            }
            gridvwAssessments.HeaderRow.Cells[columnIndex].Controls.Add(sortImage);
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为问题在于dt.Defaultview.Sort。

使用DefaultView总是让我遇到排序问题。我通常会尝试创建一个新视图。你可能想尝试一下......

    DataView dv = new DataView(dt);
    dv.Sort = String.Format("{0} {1}", e.SortExpression, Session["Assessment_SortDir"]);
    gridvwAssessments.DataSource = dv;

    // or you can use dv.ToTable(...);