net列表视图控件内选定的项目模板

net列表视图控件内选定的项目模板

本文介绍了查找Asp.net列表视图控件内选定的项目模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个asp.net的GridView转换功能到ListView。在GridView时所选择的项目改变了我会抓住从标签值的选定行中,并将其写入到一个不同的标签在GridView之外。

I'm converting functionality from an asp.net Gridview to a Listview. In the gridview when the selected item changed I would grab a value from a label in the selected row and write it to a different label outside of the gridview.

Protected Sub grdModules_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdModules.SelectedIndexChanged

    Dim lblModuleTitle As Label = grdModules.SelectedRow.FindControl("lblModuleTitle")
    lblCurrentModule.Text = lblModuleTitle.Text

End Sub

在ListView,没有一个SelectedRow概念,而是一个的SelectedItem。然而,你不能做对所选项目的FindControl。当我尝试做以下的事情(我得到一个空引用除外):

In a Listview, there isn't a "SelectedRow" concept but a SelectedItem. However you can't do findcontrol against the selected item. When I try to do the following (I get a null reference exception):

Protected Sub listviewModules_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listviewModules.SelectedIndexChanged

    Dim lblModuleTitle As Label = CType(listviewModules.FindControl("lblModuleTitle"), Label)
    lblCurrentModule.Text = lblModuleTitle.Text

End Sub

有谁知道的方式找到选择的项目模板里的控件?

Does anyone know the way to find a control inside the selected item template?

推荐答案

您正在对整个ListView控件调用的FindControl,而不只是所选择的项目。这应该工作:

You're calling FindControl on the whole ListView, rather than just the selected item. This should work:

Dim lblModuleTitle As Label = CType(listviewModules.Items(listviewModules.SelectedIndex).FindControl("lblModuleTitle"), Label)

这篇关于查找Asp.net列表视图控件内选定的项目模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:07