将变量赋值给数据绑定列表视图标签

时间:2014-06-13 18:05:52

标签: asp.net vb.net

我想将一个字符串变量分配给listview Web控件中的标签。

这是我到目前为止尝试过的代码:

Public Sub lvRecipeSteps_DataBound(sender As Object, e As EventArgs)
    Dim strRecId As String = dvRecipeItem.DataKey.Value.ToString()
    CType(lvRecipeSteps.FindControl("lblRecId"), Label).Text = strRecId.ToString()
End Sub

行:

    CType(lvRecipeSteps.FindControl("lblRecId"), Label).Text = strRecId.ToString()

返回错误:

  

对象引用未设置为对象的实例。

这是listview中的标签:

            <asp:ListView 
            ID="lvRecipeSteps" 
            runat="server" 
            DataKeyNames="Id" 
            DataSourceID="EntityDataSource_RecipeSteps" 
            InsertItemPosition="LastItem"                
            ItemPlaceholderID="itemPlaceholder"
            OnDataBinding="lvRecipeSteps_DataBinding"
            OnDataBound="lvRecipeSteps_DataBound">                                
            <AlternatingItemTemplate>
                <tr style="background-color: whitesmoke;">
                    <td class="RefineRecipe_Steps_Cells">
                        <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" CausesValidation="False" OnClientClick="return confirm('Are you sure you want to edit this record?');"/>
                        <asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" CausesValidation="False" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
                    </td>
                    <td class="RefineRecipe_Steps_Cells">
                        <asp:Label ID="lblRecId" runat="server" Visible="true"></asp:Label></td>

我做错了什么?有人可以帮忙提供帮助吗?

非常感谢。

更新1

我改为ItemDataBound

    Protected Sub lvRecipeSteps_ItemDataBound(sender As Object, e As ListViewItemEventArgs)

    Dim strRecId As String = dvRecipeItem.DataKey.Value.ToString()

    If e.Item.ItemType = ListViewItemType.DataItem Then
        Dim dataItem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
        Dim tb_1 As Label = CType(lvRecipeSteps.FindControl("lblRecId"), Label)
        tb_1.Text = strRecId.ToString
    End If
End Sub

我收到错误:对象引用未设置为对象的实例

行:tb_1.Text = strRecId.ToString

更新2

我得到它的工作,不知何故

    Protected Sub lvRecipeSteps_ItemDataBound(sender As Object, e As ListViewItemEventArgs)

    Dim strRecId As String = String.Empty
    Dim lblRecId As Label = DirectCast(e.Item.FindControl("Recipe_IdLabel"), Label)

    If e.Item.ItemType = ListViewItemType.DataItem Then
        Dim dataItem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            If Session("Id") IsNot Nothing Then
                strRecId = Session("Id")
            Else
                strRecId = dvRecipeItem.DataKey.Value.ToString()
            End If
            lblRecId.Text = strRecId.ToString()
        End If
    End If

End Sub

现在的问题是,当详细信息视图中显示的记录没有子记录(listview)时,列表视图会显示在插入模板上,但不会分配详细信息视图记录的ID,我需要id也填充了插入模板。

我需要添加什么?任何想法?

非常感谢你的帮助

更新3

我想我得到了它的工作:

    Protected Sub lvRecipeSteps_ItemDataBound(sender As Object, e As ListViewItemEventArgs)

    Dim strRecId As String = String.Empty
    Dim lblRecId As Label = DirectCast(e.Item.FindControl("Recipe_IdLabel"), Label)

    If e.Item.ItemType = ListViewItemType.DataItem Then
        Dim dataItem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)
        If e.Item.ItemType = ListViewItemType.DataItem Then
            If Session("Id") IsNot Nothing Then
                strRecId = Session("Id")
            Else
                strRecId = dvRecipeItem.DataKey.Value.ToString()
            End If
            lblRecId.Text = strRecId.ToString()
        End If
    End If

End Sub

2 个答案:

答案 0 :(得分:0)

首先要在数据绑定的列表视图项中设置标签的值,需要使用ItemDataBound事件,而不是DataBound事件。然后,您将获得针对每个项目的数据绑定触发的事件。 另外,检查ItemTemplate和AlternatingItemTemplate都有标签lblRecId。如果您有HeaderTemplate和FooterTemplate,那么只有在以下条件为真时才需要确保运行代码行

e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem

答案 1 :(得分:0)

扩展Kiran Hegde的答案和你自己的更新,你需要在ListViewDataItem中找到lblRecId控件,而不是整个ListView。由于ListView可能具有多个ListViewDataItem,因此它也可能具有多个lblRecIds。指定要在其中找到标签的ListViewDataItem应该可以获得您想要的位置。

Dim tb_1 As Label = CType(dataItem.FindControl("lblRecId"), Label)