Getting the text of a GridView HyperLink Bound Field

It's quite common to do some extra processing of data during a GridView's Row_DataBound event. You can normally capture the value of the GridView cell like this, which would capture the value of the 3rd column in the grid (row count is 0 based). 

If e.Row.RowType = DataControlRowType.DataRow Then

    Dim Value as String = CType(e.row.cells(2).Text, String)

    'some manipulation of cell data, such as formatting, etc.

    e.row.cells(2).Text = NewValue

End If

This works well if the field is configured as a BoundField: 

<asp:BoundField DataField="DataBaseFieldValue" HeaderText="Field Name" />

But if the field is configured like this, as a HyperLink field, then you must get to its value differently:

<asp:HyperLinkField DataTextField="Symbol" HeaderText="Symbol" DataNavigateUrlFields="StockID"  DataNavigateUrlFormatString="EditStock.aspx?id={0}&ref=pp" SortExpression="s.symbol" />

This assumes that the field you're looking for is the first column of the GridView control. 

If e.Row.RowType = DataControlRowType.DataRow Then

    Dim HyperLinkText as String = DirectCast(e.Row.Cells(0).Controls(0), HyperLink).Text

    Dim HypLinkURL as string = DirectCast(e.Row.Cells(0).Controls(0), HyperLink).NavigateUrl

    'Do something with the values

End If

Comments

Popular posts from this blog

Top Five Consumer Cyber Security FAQs

Diversity Programs Don’t Make Companies More Profitable

School Choice Passed by Texas Senate