namespace ContractsAndMore.Account
{
public partial class LoginPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
void OnLoggedIn(object sender, EventArgs e)
{
//Get EMPL_PK from T_EMPL and store in Session variable
var connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
ContractSystemDataContext db = new ContractSystemDataContext(connectionString);
Compiler Error Message: CS1061: 'ASP.account_login_aspx' does not contain a definition for 'OnLoggedIn' and no extension method 'OnLoggedIn' accepting a first argument of type 'ASP.account_login_aspx' could be found (are you missing a using directive
or an assembly reference?)
This won't work, because in the OnLoggedIn event, there's no current user, the current user is only available AFTER the next request. So instead of Membership.GetUser(), you need to use Membership.GetUser(Login1.Username);
Marked as answer by MParkhouse on Apr 12, 2012 09:30 PM
Adding the protected made the code compile. Thanks for that info.
However, I can not use Membership.GetUser(Login1.Username); because the UserName textbox is in a LayoutTemplate. However, I was able to use your suggestion and do the following:
However, I can not use Membership.GetUser(Login1.Username); because the UserName textbox is in a LayoutTemplate.
Did you test this or do you assume you can't? When you've a TextBox called "UserName" in your LayoutTemplate, which you have and should have, you can use Login1.UserName. How do you think the Control knows which TextBox to use for the UserName (and PassWord)?
Yes, I tested it. When I tried refering to the name of the textbox, it was not recognized as being a control on the page. I had seen a previous post on the web that mentioned the fact that controls in a LayoutTemplate couldnot be referenced directly.
When I tried refering to the name of the textbox, it was not recognized as being a control on the page.
Username is a Property of the Login control (see the link in my previous reply), and it is accesible if you're using a (layout) template or not. In When using a Layouttemplate, you need to use 2 textboxes named UserName and Password, which are used internally
by the login control to expose the UserName and Password Property. So it is NOT necessary to use findcontrol tp get the Username.
MParkhouse
had seen a previous post on the web that mentioned the fact that controls in a LayoutTemplate couldnot be referenced directly.
Ussually, this is true. But Login1.UserName isn't refering to a TextBox directly, it is the UserName Property of the Login Control, which is available during all phases of the Page LifeCycle. The Login control itself is using ViewState to pass the value
from the TextBox to the Property....
MParkhouse
Member
22 Points
93 Posts
OnLoggedIn not working
Apr 12, 2012 03:15 AM|LINK
Page directive in the .aspx page:
<%@ Page Title="" Language="C#" MasterPageFile="~/Master/ContractsAndMore.Master" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="ContractsAndMore.Account.LoginPage" %>
I've defined the Login object as shown below:
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/LoggedIn/Default.aspx" OnLoggedIn="OnLoggedIn" >
<LayoutTemplate>
In the code behind page I have the following:
namespace ContractsAndMore.Account
{
public partial class LoginPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
void OnLoggedIn(object sender, EventArgs e)
{
//Get EMPL_PK from T_EMPL and store in Session variable
var connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
ContractSystemDataContext db = new ContractSystemDataContext(connectionString);
MembershipUser currentUser = Membership.GetUser();
Guid guidUserID = new Guid(currentUser.ProviderUserKey.ToString());
T_EMPLGetEmplCobyGUIDResult emplValues = db.T_EMPLGetEmplCobyGUID(guidUserID).FirstOrDefault();
Int32 emplPK = emplValues.Empl_pk.Value;
Int32 emplCmpyFK = emplValues.EMPL_CMPY_FK.Value;
String emplCmpyName = emplValues.Cmpy_NAME;
String emplFirstName = emplValues.EMPL_FIRST_NAME;
String emplLastName = emplValues.EMPL_LAST_NAME;
Session.Add("emplPK", emplPK);
Session.Add("emplCmpyFK", emplCmpyFK);
Session.Add("emplCmpyName", emplCmpyName);
Session.Add("emplFirstName", emplFirstName);
Session.Add("emplLastName", emplLastName);
}
}
}
Can anyone tell me why it's not compiling?
Thanks
Compiler Error Message: CS1061: 'ASP.account_login_aspx' does not contain a definition for 'OnLoggedIn' and no extension method 'OnLoggedIn' accepting a first argument of type 'ASP.account_login_aspx' could be found (are you missing a using directive or an assembly reference?)
hans_v
All-Star
35986 Points
6550 Posts
Re: OnLoggedIn not working
Apr 12, 2012 08:13 AM|LINK
Try
protected void OnLoggedIn(object sender, EventArgs e)
also:
This won't work, because in the OnLoggedIn event, there's no current user, the current user is only available AFTER the next request. So instead of Membership.GetUser(), you need to use Membership.GetUser(Login1.Username);
MParkhouse
Member
22 Points
93 Posts
Re: OnLoggedIn not working
Apr 12, 2012 09:47 PM|LINK
Adding the protected made the code compile. Thanks for that info.
However, I can not use Membership.GetUser(Login1.Username); because the UserName textbox is in a LayoutTemplate. However, I was able to use your suggestion and do the following:
String userName = ((TextBox)Login1.FindControl("UserName")).Text;
MembershipUser currentUser = Membership.GetUser(userName);
Thanks very much!
hans_v
All-Star
35986 Points
6550 Posts
Re: OnLoggedIn not working
Apr 13, 2012 06:52 AM|LINK
Did you test this or do you assume you can't? When you've a TextBox called "UserName" in your LayoutTemplate, which you have and should have, you can use Login1.UserName. How do you think the Control knows which TextBox to use for the UserName (and PassWord)?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.username.aspx
MParkhouse
Member
22 Points
93 Posts
Re: OnLoggedIn not working
Apr 14, 2012 01:13 AM|LINK
Yes, I tested it. When I tried refering to the name of the textbox, it was not recognized as being a control on the page. I had seen a previous post on the web that mentioned the fact that controls in a LayoutTemplate couldnot be referenced directly.
hans_v
All-Star
35986 Points
6550 Posts
Re: OnLoggedIn not working
Apr 14, 2012 07:35 AM|LINK
Really?
Username is a Property of the Login control (see the link in my previous reply), and it is accesible if you're using a (layout) template or not. In When using a Layouttemplate, you need to use 2 textboxes named UserName and Password, which are used internally by the login control to expose the UserName and Password Property. So it is NOT necessary to use findcontrol tp get the Username.
Ussually, this is true. But Login1.UserName isn't refering to a TextBox directly, it is the UserName Property of the Login Control, which is available during all phases of the Page LifeCycle. The Login control itself is using ViewState to pass the value from the TextBox to the Property....