我是 ASP.net 的初学者,想创建一个类文件,其中包含我的应用程序中常用的方法。就像这里隐藏某些ButtonLink
's 在login.aspx
andregistration.aspx
页面中的那个。
但是当我启动这些页面中的任何一个时,我都会收到此错误:
Object reference not set to an instance of an object.
这是我的代码:
Helper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public class Helper : System.Web.UI.Page
{
public void hideLinks(){
// error is produced at the following line at the start of
// login.aspx or registration.aspx pages.
LinkButton profile = (LinkButton)Master.FindControl("LinkButton1");
LinkButton logout = (LinkButton)Master.FindControl("LinkButton2");
profile.Visible = false;
logout.Visible = false;
}
}
}
login.aspx.cs
& registration.aspx.cs
:
void Page_PreInit(object sender, EventArgs e)
{
//LinkButton profile = (LinkButton)Master.FindControl("LinkButton1");
//LinkButton logout = (LinkButton)Master.FindControl("LinkButton2");
//profile.Visible = false;
//logout.Visible = false;
Helper master_helper = new Helper();
master_helper.hideLinks();
}