Buggin' on the LoginView control

I guess I'm just lucky, but I was surprised to find some bugs with the ASP.NET LoginView control and its use in VS 2005. I know it's only beta 1, but it surprised me.

First off, I can't access controls in the LoginView. So if you have:

<asp:LoginView Runat="Server" ID="TopNav">
  <AnonymousTemplate>
    <asp:TextBox ID="EmailTextBox" Runat="server" />
  </AnonymousTemplate>
</asp:LoginView>

Then when you try to access EmailTextBox, like EmailTextBox.Text = "blah" for example, it throws an exception:
CS0103: The name 'EmailTextBox' does not exist in the current context

The thing is, it's clearly in the control tree, and if you move it outside of the LoginView, it can be accessed just fine.

Related, at some point when switching between design view and source for a page, the <LoggedInTemplate> of my LoginView migrated outside of the LoginView tags, and entirely outside of the <form> tags. It happened at some point while I was editing the <AnonymousTemplate> in design view, adding controls and an HTML table.

I guess I finally earned all of my mailings!

6 Comments

  • You can't access it like that. You have to assess it programmatically through the TopNav object, which is the ID for your LoginView control. Migfht want to switch to design view and read the context-sensitive help.

  • It is not a bug. It is by design. Basically, they are using templates which is NOT part of the control tree UNTIL the control says it is. The control normally decides the point that the controls are part of the control tree is during the PreRender stage of the life cycle.



    The better solution instead of waiting for the PreRender stage of the life cycle to come by is to migrate the controls into custom templates, port it into an user control or port it into a server control.



    I am pretty much sort of time and will respond latter with more details... maybe even write a blog post about it :-).

  • Um, guys....



    LoginControl.UserName

    LoginControl.Password



    What else do you need? You can template the control at runtime. Use a skin.

  • Um... it's not good enough?



    Not only will I not use the login control, but there's a lot more to put in there, like endless examples of customized content for a logged in user.

  • TextBox email = (TextBox)TopNav.FindControl("EmailTextBox") as TextBox;
    if (email != null)
    {
    email.Text = "blah";
    }

  • Thanks Frank, but that solution is quite ridiculous. Not your fault, but the LoginView concept seems broken IMHO.

Comments have been disabled for this content.