我在页面上有一个标签控件(test1.aspx),它有一个母版页。我想使用FindControl从test1.aspx页面(不是母版页)后面的代码访问标签控件。
我可以直接使用Button2(见下文),但我想能够使用FindControl(请参阅下面的Button1)。我得到一个NULLReference异常,我相信因为我不知道在内容控件中定位标签控件的正确语法。
在没有母版页的页面上我会使用FindControl(“MenuItemName1”),但因为控件在Content3 / ContentBody中,我相信我需要更多。任何帮助将不胜感激。
<%@ Page Title="" Language="VB" MasterPageFile="~/Shared/MasterPages/SiteLayout.Master" AutoEventWireup="false" CodeFile="Test1.aspx.vb" Inherits="Test1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="StyleSheetPage" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentBody" Runat="Server">
<asp:Label ID="lblMenuItemName1" runat="server" Text="Label"></asp:Label><br /><br />
<asp:Button ID="Button1" runat="server" Text="Button" /><br /><br />
<asp:Button ID="Button2" runat="server" Text="Button" />
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="JavaScriptPage" Runat="Server">
</asp:Content>
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim C As Content
C = CType(Me.FindControl("Content3"), Content)
Dim lblMenuItemName As Label = C.FindControl("lblMenuItemName1")
lblMenuItemName.Text = "hello"
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lblMenuItemName1.Text = "Direct"
End Sub
答案 0 :(得分:0)
找到解决方案:
Dim lblMenuItemName As Label = TryCast(Master.FindControl("ContentBody").FindControl("lblMenuItemName1"), Label)