开发者

Changing controls in Master Page in ASP.NET

开发者 https://www.devze.com 2023-03-30 09:52 出处:网络
I am designing a website that uses a master page. I have a login text box and a label a user can use to login.These controls are on my master page.

I am designing a website that uses a master page.

I have a login text box and a label a user can use to login. These controls are on my master page.

However, even after the u开发者_如何转开发ser logs in the text box and label asking for email id and password is displayed in next page where I redirect after succesful login.

After a user, I want to hide the login label and text box and instead display a welcome user message.

But I am not able to edit in next page where I redirect. how do I hide that?


If you are using FormsAuthentication, you can use LoginView to display controls according to login state of user.

<asp:LoginView ID="LoginViewTemplate" runat="server">
     <LoggedInTemplate>
         // Welcome message goes here
     </LoggedInTemplate>
     <AnonymousTemplate>
         // Textboxes and other login stuff goes here
     </AnonymousTemplate>
</asp:LoginView>


You should not have a login textbox in the master page.

If you have it for some reason, I would suggest that you have different master pages for login pages and logged in pages.


If you just want to hide it, put the controls in a panel control, then hide the panel on page load if the user is logged in.

So when the user logs in, using the login button, set a session variable for their user id like this:

Session("UserID") = <some formula to get number>

Then in the pageload code section of the master page write:

If not Session("UserID") is nothing then
    pnlLogin.Visible = False
End If

There are however better controls, and more correct ways of doing this type of action such as the login controls, so this method is not recommended.


The solution here is usually to use a templated control; this means that, depending on the status, the appropriate template, and hence controls can be displayed.

In this case, there is already the LoginView control which exposes both templates for the unauthenticated and authenticated users. By default, if will display the login template, then, when authenticated and upon reloading the page, another template will be shown which allows you to display the logged-in users name by using the LoginName control.

The links provided should give you everything you need to know to get this up and running, considering you have brought yourself this far! However, this is the snippet from the article, showing related mark-up and available properties / events, etc:

<asp:LoginView
    EnableTheming="True|False"
    EnableViewState="True|False"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    OnViewChanged="ViewChanged event handler"
    OnViewChanging="ViewChanging event handler"
    runat="server"
    SkinID="string"
    Visible="True|False">
    <AnonymousTemplate>
        <!-- child controls -->
    </AnonymousTemplate>
    <LoggedInTemplate>
        <!-- child controls -->
    </LoggedInTemplate>
    <RoleGroups>
        <asp:RoleGroup
            Roles="string">
                <ContentTemplate>
                    <!-- child controls -->
                </ContentTemplate>
        </asp:RoleGroup>
    </RoleGroups>
</asp:LoginView>

As you can see, this control provides enough to abstract to align with your own method of authentication, since there are numerous options to choose from with ASP.NET.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号