Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GridView_Sorting() Event fires twice

142 views
Skip to first unread message

RAMAN

unread,
Mar 6, 2009, 6:11:34 AM3/6/09
to
hello,
i have same problem of grid view sorting fires twice at a time.
I have set enabledviewstatte=false, AND set is not posback.
Still its firing twice.

Please help me !

Raman

From http://www.developmentnow.com/g/12_2006_6_0_0_780211/GridView-Sorting-Event-fires-twice.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/

Travis Wells

unread,
Oct 17, 2009, 2:56:18 AM10/17/09
to
I added a boolean above the actual event to catch the second time. I am not sure if it's the best thing but it does work.

Dim testSecond As Boolean = False
Protected Sub GvEmployees_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GvEmployees.Sorting
If testSecond = False Then


'Retrieve the table from the session object.
Dim dt = TryCast(Session("TaskTable"), Data.DataTable)

Dim dataView1 As New Data.DataView(dt)
Dim sortExpression As String = ViewState("LastSortExp")
Dim sortDirection As String = ViewState("LastDirec")

If (Not e.SortExpression = sortExpression) Then
sortExpression = e.SortExpression
sortDirection = "ASC"
Else
If (sortDirection = "ASC") Then
sortExpression = e.SortExpression
sortDirection = "DESC"
Else
sortExpression = e.SortExpression
sortDirection = "ASC"

End If
End If

dataView1.Sort = e.SortExpression & " " & sortDirection

GvEmployees.DataSource = dataView1
GvEmployees.DataBind()

ViewState("LastSortExp") = sortExpression
ViewState("LastDirec") = sortDirection
End If
testSecond = True

End Sub

samiup

unread,
Dec 4, 2009, 5:01:01 PM12/4/09
to
events firing twice can mean (most likely) that you are firing them twice :
1-in the front end .aspx
2-and in the code behind .vb or .cs

or in other words, you are adding the event handler twice.

C#
1-in .aspx : <asp:GridView ID="grid" OnSorting="sortingEventHandlerMethod"
.....
2-in code behind .cs :
grid.Sorting+=new GridViewSortEventHandler(sortingEventHandlerMethod);

VB:
1-in .aspx : <asp:GridView ID="grid" OnSorting="sortingEventHandlerMethod"
.....
2-Protected Sub sortingEventHandlerMethod(ByVal sender as Object, ByVal e as
GridViewSortingArgs) Handles grid.Sorting

SOLUTION:
Remove either 1 or 2 - do NOT use both.

"Travis Wells" wrote:

> .
>

0 new messages