Access shortcuts in LoginView

I use asp.net LoginView to display different data for authenticated or anonymous users.

<asp:LoginView ID="LoginView1" Runat="server">
    <LoggedInTemplate>
        <asp:Label ID="Foo" runat="server" />
    </LoggedInTemplate>
    <AnonymousTemplate>
        <asp:Label ID="Bar" runat="server" />
    </AnonymousTemplate>
</asp:LoginView>

When I moved Fooand Barto LoginView1, I could not access them from code located in this way:

Foo.Text = "I am Foo";
Bar.Text = "I am Bar";

I was able to access them this way before moving them to LoginView. What (clean) method can I use to access these tags?

+3
source share
1 answer

You need to use FindControl in LoginView and Cast respectively, as shown below:

var foo = (Label)LoginView1.FindControl("Foo");
foo.Text =  "I am Foo";
+4
source

All Articles