Loginview控件:如何在loggedintemplate中引用服务器端控件

时间:2009-03-21 16:27:13

标签: asp.net loginview

在表单的PageLoad事件中,我无法在登录模板中引用服务器端控件。我错过了什么因此,当我登录时,我将显示文本框控件,否则我将显示“请登录做soso ......”等文字。

请帮助..

2 个答案:

答案 0 :(得分:7)

你可以在loginview控件上使用FindControl方法来获取它们......

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.
}

但是,这仅适用于当前在LoginView控件的显示视图中的控件。在尝试获取文本框之前,您必须测试您是否正在显示已登录的视图,或者您还可以测试FindControl是否未返回空引用。

答案 1 :(得分:-1)

如果您仍然无法引用隐藏的对象,则可能无法为其输入正确的值。假设您有一个名为“DropDownList1”的下拉列表嵌套在loggedInView中。您必须设置一个使用DropDownList类的FindControl方法的新对象,然后使用该NEW对象:

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

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

在我的情况下,如果对象选择的值以“S”开头,我将用户重定向到新页面。

适合我,我希望它适合你!

  • Ben sewards