【问题标题】:How do I reference a GridView TemplateField label value from RowCommand?如何从 RowCommand 引用 GridView TemplateField 标签值?
【发布时间】:2018-10-11 17:58:42
【问题描述】:

我需要在以下 gridview 的选定行上引用来自 lblCaseStatus 的标签值:

<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>  

我在网上搜索过,但没有找到任何可行的解决方案。我尝试使用基于此 SO 答案 (https://stackoverflow.com/a/10784039/3938754) 的答案,其中包括此免责声明:
备注:这仅适用于 Boundfields。
我正在使用 TemplateField 并猜测这就是它失败的原因:

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

读取错误:
指定的转换无效。 (从数字转换时,该值必须是小于无穷大的数字。)
RowIndexValue 都有数据。

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

那么如何通过 RowCommand 方法引用 ItemTemplate 中的标签值?

提前感谢您的时间和帮助。

【问题讨论】:

    标签: vb.net gridview label templatefield rowcommand


    【解决方案1】:

    这个赋值将触发InvalidCastException,因为它试图将Object 值直接从DataKey.Value 属性转换为Guid 类型:

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

    您需要改用CTypeSystem.Guid 构造函数:

    ' 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))
    

    还可以使用Guid.Parse 方法来确保传递的值采用正确的 GUID 格式:

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

    或者,如果 GUID 使用某些格式(如连字符和/或用大括号括起来),请使用带有格式说明符的 Guid.ParseExact,如下例所示:

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

    附带说明,如果您想设置有关有效 GUID 值的条件,请使用 TryParseTryParseExact

    更新 1:

    由于DataKey.Value 具有Integer 类型,CType 参数需要稍作修改:

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

    或者使用自定义共享函数将Integer转换为Guid

    ' 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)
    

    【讨论】:

    • 四个 Guid 建议全部失败:1) 指定的演员表无效; 2) 无法将“System.Int32”类型的对象转换为“System.String”类型; 3) Guid 应包含 32 位数字和四个破折号 (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx); 4) 无法识别的 Guid 格式。
    • 我已经阅读了 Guid 并且不确定它如何与在 ItemTemplate 中引用标签值相关,因为 id 变量是 没有使用。我需要知道的是,“如何从 RowCommand 方法引用 ItemTemplate 中的标签值。”
    • 您引用值的方式看起来正确,但传递的值听起来不适合指定的 GUID 格式(这就是为什么您应该使用指定格式的 ParseExactTryParseExact)。你能告诉我们DataKey.Value属性究竟返回了哪种值吗?
    • 如果我正确理解您的问题,DataKey.Value 属性的类型是 Int32。
    • 如果您打算将Integer 值从DataKey 转换为Guid,您需要使用Guid 构造函数将其转换或创建共享函数来执行此操作(请参阅更新部分)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 2014-10-25
    相关资源
    最近更新 更多