Search code examples
asp.netmaster-pagesfindcontrol

masterpage with findcontrol return null


I have a master page with a label... When i try to find this label on backend it returns me null... Anyone can help me?

<div class="container">
    <div class="row" runat="server" id="Alert" visible="false">
        <div class="alert alert-danger alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span></button>
            <strong>Atenção! </strong>
            <asp:Label ID="lbAlert" for="Alert" runat="server" />
            <asp:LinkButton ID="lbkbtnAtivar" for="Alert" runat="server" />
        </div>
    </div>
</div>

and backend.

        Label lbAlert = (Label)this.Master.FindControl("lbAlert");
        LinkButton lbkbtnAtivar = (LinkButton)this.Master.FindControl("lbkbtnAtivar");

If anyone know, help me please!! :)


Solution

  • Remove "Master"

    Label lbAlert = (Label)this.FindControl("lbAlert");
    LinkButton lbkbtnAtivar = (LinkButton)this.FindControl("lbkbtnAtivar");
    

    You are already in the appropriate scope for the class. Therefore, "this" refers to the masterpage.