Page view counter

LoginView Control's ViewChanged Event does not fire

Last post 07-06-2005 4:02 AM by DonMJW. 2 replies.

Sort Posts:

  • LoginView Control's ViewChanged Event does not fire

    07-05-2005, 9:04 AM
    • Loading...
    • DonMJW
    • Joined on 07-05-2005, 8:10 AM
    • Posts 2
    • Points 10
    I've been developing a website, which consists of a master page with the login view control added to it, with a login control within the anonymous template.  On the LoggedInTemplate, I display login specific details, one of which I manually change, due to it being application specific rather than a generic ASP .NET login feature.  The master page also contains a ContentPlaceHolder.

    All of the pages within the website are based upon this master page, and have custom content, which in its simplist form is a login view control, with content defined for the Anonymous and Logged In Templates.  I have added code to handle the viewchanged event, and checked to ensure that the OnViewChanged attribute in the aspx files, and they all have the handles clause set.

    The master page handles all of the authentication of the users, which works fine.  However, when a user logs in or out, the ViewChanged event never fires on any of the loginviews, irrespective of whether they are on the master page or on the content pages.  Originally, the website was created on Beta 1, and the event fired fine, but since going over to Beta 2 (Team System), this issue has arised.

    Has anyone else experienced the same problem, and if so was there any resolution?

    Thanks for any help anyone can provide.

    Mike
  • Re: LoginView Control's ViewChanged Event does not fire

    07-05-2005, 3:03 PM
    • Loading...
    • farmas
    • Joined on 08-05-2002, 8:19 AM
    • Redmond, WA
    • Posts 228
    • Points 968
    • AspNetTeam

    Mike, the behaviour that you are describing is a design change from Beta1 to Beta2. Let me explain the new behaviour: the LoginView control will only trigger its changing events when the template to display is different than the template that it retrieved from ViewState. Think of it as the control loading a starting template upon the first request, the anonymous template if the request was anonymous or the LoggedInTemplate if the request was authenticated. In this case, the viewchange events are NOT triggered, because there was no previously selected template to change from, the control is just picking its starting template. If in a subsequent post the control needs to change templates, then the viewchange event is triggered (because its changing from the starting template to a new template).

    I attach a sample page to exemplify.

    1) The first time you hit the page (a GET request) the control chooses the AnonymousTemplate (no viewchange event)
    2) If you click on the "Login and Refresh" button, you will set the formsauth ticket and redirect back to the same page, which is in essence another GET. There is no viewstate, so the control is back to where it started, it needs to choose again its starting template, and in this case (since the formsauth ticket is present) it will select the LoggedInTemplate (no viewchange event). This is what happens when log in with the Login control
    3) Logout of the site
    4) Click on "Login" button, this will set the formsauth ticket but WILL NOT redirect
    5) Now click on "Refresh". In this case the control remembers that its starting template was the AnonymousTemplate, and now needs to change rendering to LoggedInTemplate, so it must trigger its ViewChange event.

    Notice that the Login control by default will Log in and redirect, which is why you don't get the change events. The nest result of this change is that if you need to modify the contents of the templates of LoginView, you no longer need to do it on the ViewChange event (unless your application expects authentication status change to occur on mid-request or no redirects after authentication status change). Instead you can do it on the regular Page_Load() event, because at that time the control is garanteed to have the correct template loaded up.

    Hope this helps,
    Federico Silva
    Web Platform & Tools QA Team

    <script runat="server">

        protected void LoginView1_ViewChanged(object sender, EventArgs e)
        {
            Response.Write("<br>VIEWCHANGED");
        }

        protected void LoginView1_ViewChanging(object sender, EventArgs e)
        {
            Response.Write("<br>VIEWCHANGING");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie("farmas", false);
            Response.Redirect(Request.Path);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SetAuthCookie("farmas", false);
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:LoginView ID="LoginView1" runat="server" OnViewChanged="LoginView1_ViewChanged" OnViewChanging="LoginView1_ViewChanging">
                <LoggedInTemplate>
                    YOU ARE LOGGED IN
                </LoggedInTemplate>
                <AnonymousTemplate>
                    YOU ARE ANONYMOUS
                </AnonymousTemplate>
            </asp:LoginView>
            <br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Login & Refresh" />
            <asp:Button ID="Button2" runat="server" Text="Login" OnClick="Button2_Click" />&nbsp;<asp:Button ID="Button3"
                runat="server" Text="Refresh" /><br />
            <br />
        </div>
        </form>
    </body>
    </html>

  • Re: LoginView Control's ViewChanged Event does not fire

    07-06-2005, 4:02 AM
    • Loading...
    • DonMJW
    • Joined on 07-05-2005, 8:10 AM
    • Posts 2
    • Points 10
    Many thanks for the explanation, Federico, it is very useful for understanding this change.
Page 1 of 1 (3 items)