访问listview之外的标签

时间:2012-05-31 18:46:26

标签: .net label

我在listview中使用label,在代码后面,我想访问label和
  单击按钮时设置其文本。但是当我单击按钮时,nullrefrace    exception被抛出

**.aspx Code:**

      <asp:ListView ID="ListView1" runat="server" 
      `onselectedindexchanged="labeltext"` 
            onselectedindexchanging="la">
        <ItemTemplate>  
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
      <asp:Button ID="Button1" CommandName="select" runat="server" Text="Button" />

        </ItemTemplate>
          </asp:ListView>

**.aspx.cs:**
              protected void labeltext(object sender, EventArgs e)
        {
             Label list = sender as Label;

            ListViewItem item = (ListViewItem)list.NamingContainer;
            Label lable = item.FindControl("Label2") as Label;
            lable.Text = "you click me";
        }

        protected void la(object sender, ListViewSelectEventArgs e)
        {

            Label1.Text = "rr";

        }

1 个答案:

答案 0 :(得分:0)

试试这个

ListView list = (ListView) sender;
Label lable = list.FindControl("label2") as Label;