Loginview control: how to reference server side controls inside loggedintemplate

爷,独闯天下 提交于 2019-12-05 00:05:31

问题


In PageLoad event of the form, I can not reference server side control within logged in template. What am I missing. So when I am logged in I will show text box control otherwise I will show text like "please login to do soso.."

Please help ..


回答1:


you can use the FindControl method on your loginview control to get them...

TextBox t = (TextBox)LoginView2.FindControl("TextBox1");
string s = null;

if (t != null)
{
    // textbox is in the current scope of the LoginView
    s = t.text;
}
else
{
    // the textbox is not in the current scope of the LoginView.
}

However, this will only work for the controls that are currently in the shown view of the LoginView control. You'd have to test that you're showing the logged in view before trying to grab the textbox, or you could also test that the FindControl doesn't return a null reference.




回答2:


If you're still having trouble referencing the hidden object, you might not be entering the right value for it. Say you have a drop down list called "DropDownList1" nested inside a loggedInView. You have to set a new object that uses the FindControl method of the DropDownList class, and then use that NEW object:

DropDownList d = (DropDownList)ucLogin.FindControl("DropDownList1");

       bool answer = d.SelectedValue.StartsWith("S");
       if (answer == true)
       {
           Response.Redirect("~/MemberPages/ChangePassword.aspx");
       }

In my case, I am redirecting the user to a new page if that objects selected value starts with an "S".

Works for me, and I hope it works for you!

  • Ben sewards


来源:https://stackoverflow.com/questions/669551/loginview-control-how-to-reference-server-side-controls-inside-loggedintemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!