Guys,

I'm developed a quiet complex searh page for my company and produced the results in a datagrid.

I'm following N-Tier structure so I have a view model (SortSearch.aspx), Business Logic (SortSearchManagement.vb) and Data Access Layer (SortsearchDA.vb).

I created ObjectDataSource that is getting list of objects (SearchResultEntity) and the searching conditions are stored in a session as well as sortingexpression, sorting direction and others as it shows below

HTML Code:
<asp:ObjectDataSource ID="SearchResultsObjectDataSource" runat="server" 
        SelectMethod="getRequests" TypeName="ATVS.Initiation.BLL.SortSearchManager" 
        SortParameterName="SortExpression">
        <SelectParameters>
            <asp:SessionParameter Name="searchCriteria" SessionField="SearchCriteria" 
                Type="Object" />
            <asp:SessionParameter DefaultValue="" Name="SearchType" SessionField="TYPE" 
                Type="String" />
            <asp:SessionParameter Name="UserGroup" SessionField="USERGROUP" Type="Int32" />
            <asp:SessionParameter Name="SortExpression" SessionField="SortExpression" 
                Type="String" />
            <asp:SessionParameter Name="SortDirection" SessionField="SortDirestion" 
                Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
The grid contains multiple columns and some of its columns contains buttons and hyperlink as shown below:
HTML Code:
<asp:GridView ID="resultsGrid" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" 
        DataSourceID="SearchResultsObjectDataSource" CssClass="grid" 
        AllowSorting="True">
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="ARID" DataNavigateUrlFormatString="/LastResponse.aspx?ARID={0}"
                            HeaderText="ARID" Target="_blank" DataTextField="ARID" 
                SortExpression="ARID">
                <HeaderStyle CssClass="groupHeading" />
            </asp:HyperLinkField>
            <asp:BoundField DataField="InitiatorName" HeaderText="Initiator Name" 
                SortExpression="INITIATOR_LOGIN" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="LloydeNo" HeaderText="Lloyde #" 
                SortExpression="LLOYDS_NBR" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="TankerName" HeaderText="Tanker Name" 
                SortExpression="CURRENT_NAME" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="Port" HeaderText="Port" SortExpression="PORT_NAME" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="InitiationDate" HeaderText="Initiation Date" 
                SortExpression="created_date" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="Status" HeaderText="Status" 
                SortExpression="REQUEST_STATUS_NAME" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="EvaluatorName" HeaderText="Evaluator Name" 
                SortExpression="EVALUATOR" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="DaysLeft" HeaderText="Days Left" 
                SortExpression="EXPIRATION_DATE" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:BoundField DataField="Expirydate" HeaderText="Expiry Date" 
                SortExpression="EXPIRATION_DATE" >
                <HeaderStyle CssClass="groupHeading" />
            </asp:BoundField>
            <asp:ButtonField ButtonType="Button" CommandName="Select_Override" 
                Text="Override" >
                <ControlStyle CssClass="button" />
            </asp:ButtonField>
            <asp:ButtonField ButtonType="Button" CommandName="Select_Followup" 
                Text="Follow Up" >
                <ControlStyle CssClass="button" />
            </asp:ButtonField>
            <asp:ButtonField ButtonType="Button" CommandName="Cancel" Text="Cancel" >
                <ControlStyle CssClass="button" />
            </asp:ButtonField>
            <asp:BoundField DataField="InitiatorGroup" 
                SortExpression="InitiatorGroup" />
            <asp:BoundField DataField="StatusID" SortExpression="StatusID" />
            <asp:BoundField DataField="IsInitiatorReply" 
                SortExpression="IsInitiatorReply" />
        </Columns>
        <FooterStyle HorizontalAlign="Center" />
        <HeaderStyle CssClass="groupHeading" />
    </asp:GridView>
I turned autosorting on and wrote a cutomized sorting event as shown below
Code:
    Protected Sub resultsGrid_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles resultsGrid.Sorting
        Session("SortDirestion") = CInt(e.SortDirection)
        Session("SortExpression") = e.SortExpression.ToString()
        resultsGrid.DataBind()
    End Sub
I have two issues:
  1. The session value SortExpression is passing nothing to Data Access Layer? While it is passing all others?
  2. Once I click on a column header every thing went well but once the grid is rendering it disappears?!!


Here is the Page load event for the aspx file
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsNothing(Session("USERGROUP")) Then
            Response.Redirect("Default.aspx")
        End If
        Session("TYPE") = Request.QueryString("TYPE")

        resultsGrid.Visible = False
        If Session("TYPE") = "" Then
            Session("TYPE") = "ALL"
        End If
        Dim type As String = Session("TYPE")
        If type.Equals("FOLLOWUP") Or type.Equals("OVERRIDE") Then
            statusDropDownList.Visible = False
            StatusLabel.Visible = False
        End If
        If Not IsPostBack Then
            Session("SortDirestion") = 0
            Session("SortExpression") = "ARID"
        End If
    End Sub
Here are all events for GridView control
Code:
    
Protected Sub resultsGrid_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles resultsGrid.PageIndexChanging
        resultsGrid.PageIndex = e.NewPageIndex
        'resultsGrid.DataBind()
        startSearch()
    End Sub
    Private Sub resultsGrid_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles resultsGrid.PreRender
        If resultsGrid.Rows.Count = 0 Then
            ErrLabel.Visible = True
            ErrLabel.Text = "There is no result matching your search criteria"
        Else
            ErrLabel.Visible = False
        End If
        'If Results.Count > 0 Then
        populateResultGrid(Session("USERGROUP"))
        'End If
    End Sub
Protected Sub resultsGrid_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles resultsGrid.RowCommand
        Dim x As Integer = 0
        If e.CommandName.Equals("Select_Override") Then
            x = CInt(e.CommandArgument)
            Dim HyperLinkFromGrid As New HyperLink
            HyperLinkFromGrid = resultsGrid.Rows(x).Cells(0).Controls(0)
            Dim ARID As String = HyperLinkFromGrid.Text
            Session("ARID") = ARID
            Session("PORT") = resultsGrid.Rows(x).Cells.Item(4).Text
            Session("OverrideDataTableCreationFlag") = False
            Session("CurrentReqStatusID") = CInt(resultsGrid.Rows(x).Cells.Item(resultsGrid.Columns.Count - 2).Text)
            'Destroy all Sessions
            Session("TYPE") = Nothing
            Session("SearchCriteria") = Nothing
            Response.Redirect("Override_Request.aspx")
        ElseIf e.CommandName.Equals("Select_Followup") Then
            x = CInt(e.CommandArgument)
            Dim HyperLinkFromGrid As New HyperLink
            HyperLinkFromGrid = resultsGrid.Rows(x).Cells(0).Controls(0)
            Dim ARID As String = HyperLinkFromGrid.Text
            Session("ARID") = ARID
            Session("PORT") = resultsGrid.Rows(x).Cells.Item(4).Text
            Dim status As Integer = CInt(resultsGrid.Rows(x).Cells.Item(14).Text)
            'Destroy all Sessions
            Session("TYPE") = Nothing
            Session("SearchCriteria") = Nothing
            If status = 3 Then
                Response.Redirect("Followup_Request.aspx")
            ElseIf status = 9 Then
                Response.Redirect("OverrideFollowupRequest.aspx")
            End If
        ElseIf e.CommandName.Equals("Cancel") Then
            x = CInt(e.CommandArgument)
            Dim HyperLinkFromGrid As New HyperLink
            HyperLinkFromGrid = resultsGrid.Rows(x).Cells(0).Controls(0)
            Dim ARID As String = HyperLinkFromGrid.Text
            Session("ARID") = ARID
            'Destroy all Sessions
            Session("TYPE") = Nothing
            Session("SearchCriteria") = Nothing
            Response.Redirect("CancelRequest.aspx")
        End If
    End Sub