Home > Mobile >  SortExpression not working in ASP.NET If Header Text is passed dynamically through VB code
SortExpression not working in ASP.NET If Header Text is passed dynamically through VB code

Time:01-19

I am using below aspx code for Column 6(This column number has reference in .vb code and code is mentioned below aspx code):

<asp:BoundField ControlStyle-CssClass="dbody" DataField="order_date" HeaderText="Order Date" SortExpression="order_date">                            
                                <HeaderStyle HorizontalAlign="Left" ForeColor="White" />
                                <ItemStyle HorizontalAlign="Left" />
                            </asp:BoundField>

On aspx.vb page I am using below code to pass dynamically Header Text:

 If iOrderType = 4 Then
                        gvOrders.HeaderRow.Cells(6).Text = "DVN date" 
                    ElseIf iOrderType = 11 Then
                        gvOrders.HeaderRow.Cells(6).Text = "Lab Order Date" 
                    Else
                        gvOrders.HeaderRow.Cells(6).Text = "Order Date" 
                    End If

Now after running application my sixth column is not working as sorting expression link. Also in asp grid view tag AllowSorting is "true" and gvOrder is grid id.

Grid Header Image

CodePudding user response:

For a header cell with sorting enabled, the DataControlField will add a LinkButton to the TableCell, and set its properties accordingly.

When you set the TableCell's Text property, it removes all of the controls which were created by the DataControlField. This includes the LinkButton used to sort the cell.

You need to change the text of the LinkButton instead.

Dim text As String
If iOrderType = 4 Then
    text = "DVN date" 
ElseIf iOrderType = 11 Then
    text = "Lab Order Date" 
Else
    text = "Order Date" 
End If

Dim cell As TableCell = gvOrders.HeaderRow.Cells(6)
Dim button As IButtonControl = If(cell.HasControls(), TryCast(cell.Controls(0), IButtonControl), Nothing)
If button Is Nothing Then
    cell.Text = text
Else
    button.Text = text
End If
  •  Tags:  
  • Related