排序gridview列时会引发stackoverflow异常 [英] Throws stackoverflow exception when sorting a gridview column

查看:91
本文介绍了排序gridview列时会引发stackoverflow异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,我正在尝试对列进行排序。当我这样做时,它抛出'StackOverflow'异常



我尝试过:



aspx代码:



I have a gridview and I'm trying to do Sorting on a Column. When I do that, it throws 'StackOverflow' Exception

What I have tried:

aspx Code:

<asp:GridView ID="grdItems" runat="server"  Width="100%" AllowPaging="True"
                CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" OnRowDataBound="grdItems_RowDataBound" AllowSorting="True" OnSorting="grdItems_Sorting">
                <FooterStyle BackColor="White" Font-Bold="True" ForeColor="#808080"/>
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <pagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" Font-Size="Small" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
                <Columns>

                    <asp:BoundField DataField="actionItemId" HeaderText="Item Id" SortExpression="actionItemId" >
                        <ItemStyle Font-Size="Small" VerticalAlign="Top" />
                        <HeaderStyle Font-Bold="True" Font-Size="Small" HorizontalAlign="Left" Width="65px"/>
                        <FooterStyle Font-Size="12px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="dueDate" DataFormatString="{0:d}" HeaderText="Due By" HtmlEncode="False" >
                        <ItemStyle Font-Size="Small" VerticalAlign="Top" />
                        <HeaderStyle Font-Size="Small" HorizontalAlign="Left" Width="65px" />
                    </asp:BoundField>
</Columns>
            <pagerSettings Mode="NumericFirstLast" />
        </asp:GridView>





代码背后:





Code Behind:

Protected Sub grdItems_Sorting(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdItems.Sorting
        Dim sortExpression As String = e.SortExpression
        ViewState("SortExpression") = sortExpression

        If grdItems.SortDirection = WebControls.SortDirection.Ascending Then
            grdItems.Sort(sortExpression, WebControls.SortDirection.Descending)
        Else
            grdItems.Sort(sortExpression, WebControls.SortDirection.Ascending)
        End If

    End Sub





现在,我知道,因为我在Sorting事件处理程序中使用Sort(),所以它抛出了我的异常。还有其他方法吗?任何帮助将不胜感激。谢谢!



Now, I know that, since I'm using Sort() inside the Sorting event handler, it's throwing me that exception. Is there any other way around this? Any help would be greatly appreciated. Thanks!

推荐答案

简单:不要在Sorting事件处理程序中调用Sort - 因为它会引发一个Sorting事件,它将调用Sort方法,提升排序事件,... ...

你明白了。



为什么你要在排序中排序呢? ?
Simple: don't call Sort inside the Sorting event handler - because it'll raise a Sorting event, which will call the Sort method, which will raise the Sorting event, which ...
You get the idea.

Why would you want to sort in the middle of sorting anyway?


您不需要对排序事件进行排序。你只需用新的sortexpression绑定网格。





不需要以下内容:

You don't need to sort inside sorting event. You just bind the grid with new sortexpression.


Following is not needed:
grdItems.SortDirection = WebControls.SortDirection.Ascending Then
           grdItems.Sort(sortExpression, WebControls.SortDirection.Descending)
       Else
           grdItems.Sort(sortExpression, WebControls.SortDirection.Ascending)
       End If





写下这样的东西:



Write something like this instead:

If grdItems.SortDirection = WebControls.SortDirection.Ascending Then
	grdItems.sortexpression =sortExpression + " " +  WebControls.SortDirection.Descending
Else
	grdItems.sortexpression =sortExpression + " " +  WebControls.SortDirection.Ascending
End If
grdItems.databind()


这篇关于排序gridview列时会引发stackoverflow异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆