单击asp linkbutton后保持gridview排序

时间:2018-04-25 15:19:55

标签: c# asp.net sorting gridview

我已经尝试了许多解决方案来实现这一点,但我找不到一个适合我的解决方案。我是使用ASP.net进行C#编程的初学者,我很感激能得到的任何帮助。

目前,我的gridview允许我排序,如果我点击列标题,并按升序降序排序。但是,当我单击一个链接按钮,即我的程序的'flags'列中的一个按钮时,它会突出显示该选项(它应该这样做),但它不保持排序 - 我希望它单击此后保持排序。有人能帮忙吗?

这是我的aspx代码(这是在asp:GridView中):

<%--Flags column--%>                    
                <asp:TemplateField HeaderText ="Flags" ItemStyle-Width="34px" SortExpression="sortFlag">
                <ItemTemplate>
                    <div style="width:34px">
                        <asp:HiddenField ID="HidSortFlag" Value='<%#Eval("sortFlag") %>' runat="server" />
                        <asp:HiddenField ID="hidAmended" Value='<%#Eval("Amended") %>' runat="server" />
                        <asp:HiddenField ID="hidPayAnyway" Value='<%#Eval("PaymentOverrideFlag") %>' runat="server" />
                        <asp:HiddenField ID="hidQueries" Value='<%#Eval("QueryCount") %>' runat="server" />
                        <asp:HiddenField ID="hidSupplierRefCount" Value='<%#Eval("SupplierRefCount") %>' runat="server" />
                        <asp:HiddenField ID="hdnElementRef" Value='<%# Eval("SuggestedMatches") %>' runat="server" />
                        <asp:LinkButton ID="flagQueries" runat="server" Text="<span class='glyphicon glyphicon-comment' data-toggle='tooltip' data-placement='top' title='Comments and Queries'></span>" OnClick="flagQueries_Click" CommandArgument='<%# Eval("StatementLineId") +";"+Eval("SupplierRef") +";" + Eval("SuggestedMatches")%>' > </asp:LinkButton><br />
                        <asp:LinkButton ID="flagAmends" runat="server" Text="<span class='glyphicon glyphicon-flag' data-toggle='tooltip' data-placement='top' title='Related Booking Has Been Amended' ></span>"  OnClick="flagAmends_Click" CommandArgument='<%# Eval("StatementLineId")  %>'></asp:LinkButton><br />
                        <asp:LinkButton id="flagPayments" runat="server" Text="<span class='glyphicon glyphicon-ok' data-toggle='tooltip' data-placement='top' title='Pay Anyway'></span>" OnClick="flagPayments_Click" CommandArgument='<%# Eval("StatementLineId") + ";" +Eval("SuggestedMatches")%>'></asp:LinkButton> <br />

                    </div>                         
                </ItemTemplate>
                </asp:TemplateField>  

以下是代码的代码:

        protected void gridSummary_Sorting(object sender, GridViewSortEventArgs e)
    {
        //Check if the sort field is being used or a new sort
        if (ViewState["summarySortField"] == null)
        {
            ViewState["summarySortField"] = e.SortExpression;
        }
        else if (ViewState["summarySortField"].ToString() != e.SortExpression)
        {
            ViewState["summaryDirectionState"] = null;
            ViewState["summarySortField"] = e.SortExpression;
        }

        summaryDirection = summaryDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
        string sortingDirection = summaryDirection == SortDirection.Ascending ? "Desc" : "Asc";

        DataView summaryView = LoadStatement();

        summaryView.Sort = string.Format("{0} {1}", e.SortExpression, sortingDirection);
        //Session["SortedView"] = summaryView;
        gridSummary.DataSource = summaryView;
        gridSummary.DataBind();
        //ColourFlags();
    }
    public SortDirection summaryDirection
    {
        get
        {
            if (ViewState["summaryDirectionState"] == null)
            {
                ViewState["summaryDirectionState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["summaryDirectionState"];
        }
        set
        {
            ViewState["summaryDirectionState"] = value;
        }
    }

    protected void gridDetail_Sorting(object sender, GridViewSortEventArgs e)
    {
        //Check if the sort field is being used or a new sort
        if (ViewState["detailSortField"] == null)
        {
            ViewState["detailSortField"] = e.SortExpression;
        }
        else if (ViewState["detailSortField"].ToString() != e.SortExpression)
        {
            ViewState["detailDirectionState"] = null;
            ViewState["detailSortField"] = e.SortExpression;
        }

        detailDirection = detailDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
        string sortingDirection = detailDirection == SortDirection.Ascending ? "Desc" : "Asc";

        //string gets toggle error field in order to add the colour red to certain columns
        DataView detailView = LoadDetails(Session["arg"].ToString());
        string[] args = Session["arg"].ToString().Split(';');
        string status = args[0];

        detailView.Sort = string.Format("{0} {1}", e.SortExpression, sortingDirection);
        //Session["SortedView"] = summaryView;
        gridDetail.DataSource = detailView;
        gridDetail.DataBind();
        ColourFlags();
        ToggleErrorFields(string.IsNullOrEmpty(status));
    }

    public SortDirection detailDirection
    {
        get
        {
            if (ViewState["detailDirectionState"] == null)
            {
                ViewState["detailDirectionState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["detailDirectionState"];
        }
        set
        {
            ViewState["detailDirectionState"] = value;
        }
    }

以下是其中一个标志的点击事件:

       protected void flagAmends_Click(object sender, EventArgs e)
    {
        LinkButton faBtn = sender as LinkButton;
        int lineId = int.Parse(faBtn.CommandArgument);

        using (SpamEntities spam = new SpamEntities())
        {
            SPM_Statement_Lines line = spam.SPM_Statement_Lines.Where(x => x.StatementLineID == lineId).FirstOrDefault();

            line.Amended = !line.Amended;
            spam.SaveChanges();
        }

        //reloads gridview
        LoadDetails(hidStatus.Value + ";" + hidCategory.Value + ";" + lineId);            
    }

按钮点击后按钮改变颜色,并执行必要的操作,但它没有保持其排序 - 我一直试图解决这个问题,但我没有成功。我会很感激任何明确的一步一步的解决方案。

0 个答案:

没有答案