Displaying Username when logged in

Posted by Mongz under ASP.NET on 9/16/2009 | Views : 6330 | Status : [Member] | Replies : 4
Hi guys
Here im using mater page, inside it there's a content page which has a login control, i need to display username once user logged on, on top of master page so that it will always be displayed while the user is online. I need to retrieve the username from the database which i created myself, the problem arrise when im coding behind content page where by i cant call the Username display. How can i interact with the login view which is on the master page on a different content page.
Please help to resolve this.




Responses

Posted by: perfectchourasia-9163 on: 11/17/2011 [Member] Starter | Points: 25

Up
0
Down
in login page keep the username in session and use this



ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Mongz, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/16/2009 [Member] [MVP] Bronze

Up
0
Down
Firstly you have to add a mastertype declaration to the top of any of the content pages you want to access your masterpage from.

You need to put it on the line after your @Page declaration.

If your master page was called MasterPage.master then it would look like this:

<%@ MasterType VirtualPath="~/MasterPage.master" %>


After that you can access your masterpage in the code behind by using

master.


If you add a public property to your usercontrol in the masterpage code behind and you called it LoginControl then you could use code like the following:

string username = master.LoginControl.UserName;


The code for your masterpage public property would look something like:

    public logincontrol LoginControl

{
get { return myLoginControl; }
}


Where:
logincontrol is the name of the login control class
LoginControl is the name of your public property
myLoginControl is the id of the login control on the masterpage.

Mongz, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mouli on: 9/23/2009 [Member] Starter

Up
0
Down
In the Login page during post of the Login button click, get the user name and store in the Session - Session["UserName"]

In your master page, if the Session["UserName"] is not null, display them in the label.

Mongz, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rtpharry on: 9/24/2009 [Member] [MVP] Bronze

Up
0
Down
Or alternatively you can use the built in asp.net membership technology. Even if you want to use your own database you can still use this technology and create something called a custom membership provider. Its not very complicated to do and if you search "asp.net custom membership provider" you will find many step by step tutorials.

If you did that then you would be able to access the logged in username from any page with some code like this:

string username = System.Web.HttpContext.Current.User.Identity.Name;


or by using the loginname control:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.loginname.aspx

Mongz, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response