Reusing code for multiple GridViews on same page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stoogots2
    New Member
    • Sep 2007
    • 77

    Reusing code for multiple GridViews on same page

    I would like to reuse the code for gridview sorting for each of the several GridViews that I have on one page. I've not done this before, so I am seeking a more elegant and reusable solution than coding events for each of the 4 gridviews I have.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Have you implemented a method that sorts which you can call when ever a GridView needs to be sorted?

    Comment

    • stoogots2
      New Member
      • Sep 2007
      • 77

      #3
      I am doing "custom" sorting so yes I have. The GridviewSortEve ntArgs do not specify the Gridview itself, so I'm curious how to tell the event handler which GridView to sort.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Ok, you've created a method that does sorting.

        One of the parameters that your method requires should be a reference to the GridView that called the method.

        For example:
        Code:
        Private Sub MyGlobalSortingMethod(ByVal theGridView As GridView, ByVal theSortParameters As GridviewSortEventArgs)
        
         'Sorting code
        
        End Sub
        Please note that I used ByVal in the above code.
        The reason for this is because a GridView is an Object, which means that it's memory location is passed as the value....

        If you're not using VB.NET, just ignore this.

        Comment

        • stoogots2
          New Member
          • Sep 2007
          • 77

          #5
          OK, that helps, except that I don't know how to get the GridView control's name from the postback. What I am trying to do is have OnSorting point to the same method for 4 out of 5 gridviews on one page.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            The "sender" parameter should be the GridView.

            Comment

            • stoogots2
              New Member
              • Sep 2007
              • 77

              #7
              Insert embarrassed smile here. Thanks for your help.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                No problem :)
                Don't feel bad, I used to make the same mistakes when I first started using .NET.
                It just takes some getting used to :)

                Comment

                • bhupinder
                  New Member
                  • Feb 2009
                  • 32

                  #9
                  Reusing code for multiple GridViews on same page

                  Hi
                  I have already used this code. firstly , i had to face same problem. but
                  now is working properly. I think, your problem is same. You can make a common function and call on sorting. In this function you have to pass argument according to each gridview.

                  Code:
                   Protected Sub gv1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv1.Sorting
                  
                          Dim colum As String = e.SortExpression
                          sorting(colum)
                          binddata1()
                  
                  
                  
                      End Sub
                  
                    
                      Function sorting(ByVal colum As String)
                          hiddenSortColumnLeads.Value = colum
                          If hiddenSortOrderLeads.Value = "asc" Then
                              hiddenSortOrderLeads.Value = "desc"
                          Else
                              hiddenSortOrderLeads.Value = "asc"
                          End If
                      End Function

                  Implement this code.
                  Last edited by Frinavale; Feb 25 '09, 03:47 PM. Reason: Added [code] tags: Please post code between [code][/code] tags

                  Comment

                  Working...