Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

c# - Find control in loginview

enter image description hereI have a masterpage with a loginview, in the loginview i have an asp login control. i also have a label that is NOT contained in a loginview.

How do i access the username textbox control in the asp login control and display the text in a label.

Please help!

This is my code:

Login lg = (Login)LoginView1.FindControl("Login1");
TextBox tb = (TextBox)lg.FindControl("UserName");

Label2.Text = tb.Text;

ok this is what i need to do: A complainant can deactivate his account. A manager and technicians accounts are deactivated and if necessary also reactivated by the administrator. the complainant can reactivate his account at any time.

i need to validate the username entered in the textbox to first check if it is an active user. if not it allows them to reactivate it. how do i access the text from the username textbox. the rest i can figure out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe you must check if the user is authenticated or not, because the TextBox is inside the AnonymousTemplate or it's a Namespace issue(WebControls.Login):

if (!HttpContext.Current.User.Identity.IsAuthenticated) {
   Login lg = (WebControls.Login)LoginView1.FindControl("Login1");
   TextBox tb = (TextBox)lg.FindControl("UserName");
   Label2.Text = tb.Text;
}

But normally you would get the UserName/Password via the appropriate properties UserName/Password of the Login.

Edit: Your added screenshot is very small but i see that you are getting an InvalidCastException, so my asumption on the namespace issue was correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...