How do I reference a GridView TemplateField label value from RowCommand?

452 Views Asked by At

I need to reference the label value from lblCaseStatus on the selected row of the following gridview:

<asp:GridView ID="grdTaskList" runat="server" DataKeyNames="CaseID"
              AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True"
              PageSize="20">
    <Columns>
        <asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task" 
                        ItemStyle-Width="350px" />
        <asp:BoundField DataField="DueDate" HeaderText="Due Date" SortExpression="DueDate"
                        DataFormatString="{0:d}" />
        <asp:TemplateField HeaderText="Case Status">
            <ItemTemplate>
                <asp:Label ID="lblCaseStatus" runat="server"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="btnView" runat="server" Text="View"
                                CommandName="ViewIntake"
                                CommandArgument='<%# Eval("CaseID") %>' 
                                Font-Bold="true" />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
    </asp:TemplateField>
    </Columns>
</asp:GridView>  

I have searched the web and I have not found any solutions that will work. I tried using one based on this SO answer (https://stackoverflow.com/a/10784039/3938754) which included this disclaimer:
Remark: this only works with Boundfields.
I am using a TemplateField and guessing this is why it fails on the line:

Dim id as Guid = grdTaskList.DataKeys(row.RowIndex).Value

with the error reading:
Specified cast is not valid. (When casting from a number, the value must be a number less than infinity.)
Both the RowIndex and Value have data.

Private Sub grdTaskList_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdTaskList.RowCommand
    If (e.CommandName = "ViewIntake") Then
        Dim caseID As Integer = Int32.Parse(e.CommandArgument.ToString())

        Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
        Dim id As Guid = grdTaskList.DataKeys(row.RowIndex).Value
        Dim caseStatus As String = CType(row.Cells(2), DataControlFieldCell).Text

        Response.Redirect(IntakeSite.EditIntake.GetPageURL(caseID:=caseID, caseStatus:=caseStatus))
    End If
End Sub

So how do I reference the label value inside an ItemTemplate from the RowCommand method?

Thanks in advance for your time and assistance.

1

There are 1 best solutions below

5
Tetsuya Yamamoto On

This assignment will trigger InvalidCastException because it tries to convert Object value directly from DataKey.Value property to Guid type:

Dim id As Guid = grdTaskList.DataKeys(row.RowIndex).Value 'throws InvalidCastException

You need to use CType or System.Guid constructor instead:

' using type conversion
Dim id As Guid = CType(grdTaskList.DataKeys(row.RowIndex).Value, System.Guid)

' alternative with Guid constructor
Dim id As Guid = New Guid(DirectCast(grdTaskList.DataKeys(row.RowIndex).Value, String))

Also Guid.Parse method can be used to ensure that passed value is in proper GUID format:

Dim id As Guid = Guid.Parse(grdTaskList.DataKeys(row.RowIndex).Value.ToString())

Or if the GUID uses certain formatting like hyphens and/or enclosed with braces, use Guid.ParseExact with format specifier as in example below:

'example format: 00000000-0000-0000-0000-000000000000
Dim id As Guid = Guid.ParseExact(grdTaskList.DataKeys(row.RowIndex).Value.ToString(), "D")

As a side note, use TryParse or TryParseExact if you want to put conditions regarding valid GUID value.

Update 1:

Since DataKey.Value has Integer type, CType argument needs slightly modified:

Dim id As Guid = New Guid(CType(grdTaskList.DataKeys(row.RowIndex).Value, Integer))

Or use Integer to Guid conversion with custom shared function:

' other module
Public Shared Function ToGuid(ByVal value As Integer) As Guid
   Dim bytes As Byte() = New Byte(16)
   BitConverter.GetBytes(value).CopyTo(bytes, 0)
   Return New Guid(bytes)
End Function

' RowCommand method
Dim val As Integer = CType(grdTaskList.DataKeys(row.RowIndex).Value, Integer)
Dim id As Guid = ToGuid(val)