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.
This assignment will trigger
InvalidCastExceptionbecause it tries to convertObjectvalue directly fromDataKey.Valueproperty toGuidtype:You need to use
CTypeorSystem.Guidconstructor instead:Also
Guid.Parsemethod can be used to ensure that passed value is in proper GUID format:Or if the GUID uses certain formatting like hyphens and/or enclosed with braces, use
Guid.ParseExactwith format specifier as in example below:As a side note, use
TryParseorTryParseExactif you want to put conditions regarding valid GUID value.Update 1:
Since
DataKey.ValuehasIntegertype,CTypeargument needs slightly modified:Or use
IntegertoGuidconversion with custom shared function: