Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 05, 2012 04:07 PM by cjstg
Apr 16, 2007 01:52 AM|LINK
I will be honest, this issue is madning time wasting and makes me want to throw .net out of the window.
I have been developing in .net for about 5 years and consider myself an advanced user, I did classic asp for 5 years before that. I have spent all weekend just trying to configure these login controls to work, when I could have written the whole thing in
classic in about a day.
I have the LoginView > AnonymousTemplate > Login control hierarchy as below...
<asp:LoginView ID="LoginView1" Runat="server">
<asp:Login id="Login1" runat="server">
My users fall into several different roles and have different home pages. Thus when they login, I need to redirect to a different folder/page. To do this I added the OnLoggedIn="Login1_LoggedIn" event handler to the <asp:Login > control as below...
<asp:Login id="Login1" runat="server" OnLoggedIn="Login1_LoggedIn">
The problem is in the code behind, I cannot access the username from the event handler code (protected void Login1_LoggedIn(object sender, EventArgs e)). Remember Login1.UserName does not work because the Login control lives in the LoginView and AnonymousTemplate
control. I have tried the following... I saw it in an earlier post, but username is not available on the loginControl variable.
protected void Login1_LoggedIn(object sender, EventArgs e)
Login loginControl = (Login)LoginView1.Controls.Controls.FindControl("Login1");
TextBox txtUsername = (TextBox)loginControl.UserName;
Basically how can I redirect a user based on their role and the fact that the Login1 control is not accessible. I hope someone can help, otherwise .net might be losing another friend.
username LoginView AnonymousTemplate Login controls destinationpageurl
Apr 17, 2007 11:07 AM|LINK
For the LoginView control, when being added onto a page, at a certain time,
only one Template (anonymous or loggedIn ) is applied on the Control instance, so at that time, we can only retrieve the reference of those controls in the active template( can't access those in the non-active template).
So you can first determine whether the user has been authenticated or not and then use the LoginView.FindControl( stringId )
Here is an example and it works well, Hope it helps.
Login lg = (Login)LoginView1.Controls.Controls.FindControl("Login2");
TextBox tb = (TextBox)lg.FindControl("UserName");
tb.Text = "set username";
Apr 17, 2007 02:38 PM|LINK
I got this issue resolved using the following. A friend showed my how to use Quick Watch to view the control hierarchy, and we determined the username, text control could be accessed by using the following
--> string txtUsername = ((System.Web.UI.WebControls.TextBox)(LoginView1.Controls.Controls.Controls.Controls)).Text;. <--
Your hierarchy may be different than mine so the code above may not work for you, but just use Quick Watch to find the username control in the control heirarchy and copy the path. Also you may have to replace private control names like _control with Control.
public void Login1_LoggedIn(object sender, EventArgs e)
//Login loginControl = LoginView1.Controls.FindControl("Login1") as Login;
string txtUsername = ((System.Web.UI.WebControls.TextBox)(LoginView1.Controls.Controls.Controls.Controls)).Text;
else if(Roles.IsUserInRole(txtUsername, "Employer"))
Apr 18, 2007 01:38 AM|LINK
I am glad to hear you have straightened everything out.
Thank you for your advice. It is so helpful to us
May 04, 2007 11:31 PM|LINK
May 04, 2007 11:56 PM|LINK
Post your code and maybe I can help, I recently spent some time trying figure this thing out...
As for using quickwatch, I have been just setting a breakpoint, then mousing over the control, login for example. You can follow the path to the internal control doing this...
Dec 05, 2007 05:20 PM|LINK
Thanks for this post -- I have been struggling with this exact same scenario using a login control in an anonymous template. I ran across the following on another forum and it works for me. Since the sender of the loggedin event is the login control, you
can cast the sender object to a login object and then access it to get the username.
protected void LoginControl_LoggedIn(object sender, EventArgs e)
Login loginControl = (Login)sender;
Mar 25, 2008 02:11 AM|LINK
I hope you find this useful, It worked with me. Beside I didn't like this mess about .Control.Control...
this is much easier and it working perfectly.
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
Login Login1 = ((Login)LoginView1.FindControl("Login1"));
string userName = Login1.UserName.ToString();
string password = Login1.Password.ToString();
if (Membership.ValidateUser(userName, password))
if (Request.QueryString["ReturnUrl"] != null)
Label lblResults = (Label)Login1.FindControl("lblResults");
lblResults.Visible = true;
lblResults.Text = "regarding that you have only 5 times to try";
if ((Membership.GetUser(userName) != null) && (Membership.GetUser(userName).IsLockedOut == true))
lblResults.Text = " <b>Your account has been locked out.</b>";
Dont forget to mark it as answer if it helps !
Apr 02, 2008 01:58 PM|LINK
Exactly what I was looking for!!
May 08, 2008 05:06 AM|LINK
i have a function in my base page class. in this case I call:
Dim obj As Object = Me.FindControlDeep(Me, "UserName")
Protected Function FindControlDeep(ByRef Parent
As Control, ByVal strId
As String) As Object
Dim o As Object
o = Parent.FindControl(strId)
If o IsNot Nothing Then
Dim oc As Control
For Each oc In Parent.Controls
o = FindControlDeep(oc, strId)
If o IsNot Nothing Then