在表单的 PageLoad 事件中,我无法在登录的模板中引用服务器端控件。我错过了什么。因此,当我登录时,我将显示文本框控件,否则我将显示诸如“请登录以执行此操作..”之类的文本
请帮忙 ..
您可以在 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 不返回空引用。
如果您在引用隐藏对象时仍然遇到问题,则可能没有为它输入正确的值。假设您有一个名为“DropDownList1”的下拉列表嵌套在loggedInView 中。您必须设置一个使用 DropDownList 类的 FindControl 方法的新对象,然后使用该新对象:
DropDownList d = (DropDownList)ucLogin.FindControl("DropDownList1");
bool answer = d.SelectedValue.StartsWith("S");
if (answer == true)
{
Response.Redirect("~/MemberPages/ChangePassword.aspx");
}
就我而言,如果该对象选择的值以“S”开头,我会将用户重定向到新页面。
对我有用,我希望它对你有用!