C# 使用函数设置母版页文件
我想使用另一个类函数更改masterpagefile 例如:C# 使用函数设置母版页文件,c#,asp.net,C#,Asp.net,我想使用另一个类函数更改masterpagefile 例如: public class XYZClass { public void CheckLogin(object ses,bool ipb,ref MasterPage page) { if (!(ses == null)) { if (ses.ToString() == "Admin") page.MasterPageFile = "
public class XYZClass
{
public void CheckLogin(object ses,bool ipb,ref MasterPage page)
{
if (!(ses == null))
{
if (ses.ToString() == "Admin")
page.MasterPageFile = "~/Admin.master";
else
page.MasterPageFile = "~/MasterPage.master";
}
else
{
Response.Redirect("~/frmLogin.aspx");
}
}
}
public partial class frmDoctorHistory : System.Web.UI.Page
{
BLLcheckLogin checkLogin = null;
protected void Page_PreInit(object sender, EventArgs e)
{
checkLogin = new BLLcheckLogin();
checkLogin.CheckLogin(Session["usertype"], IsPostBack, ref MasterPageFile);
}
}
我从另一个aspx源文件调用这个函数,它会给我一个错误,比如
“属性、索引器或动态成员访问权限不能作为out或ref参数传递。”您可以在
PreInit
事件之前设置母版页
请参阅中的代码示例作为参考
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
您必须将当前代码重写为:
public void CheckLogin(object ses,bool ipb, Page page)
{
if (!(ses == null))
{
if (ses.ToString() == "Admin")
page.MasterPageFile = "~/Admin.master";
else
page.MasterPageFile = "~/MasterPage.master";
}
else
{
Response.Redirect("~/frmLogin.aspx");
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
checkLogin = new BLLcheckLogin();
MasterPage mp;
checkLogin.CheckLogin(Session["usertype"], IsPostBack, this);
}
我将您的代码更改为传入
页面
,而不是主页面文件
属性。此时不再需要Ref。您可以在PreInit
事件之前设置母版页
请参阅中的代码示例作为参考
void Page_PreInit(Object sender, EventArgs e)
{
this.MasterPageFile = "~/NewMaster.master";
}
您必须将当前代码重写为:
public void CheckLogin(object ses,bool ipb, Page page)
{
if (!(ses == null))
{
if (ses.ToString() == "Admin")
page.MasterPageFile = "~/Admin.master";
else
page.MasterPageFile = "~/MasterPage.master";
}
else
{
Response.Redirect("~/frmLogin.aspx");
}
}
protected void Page_PreInit(object sender, EventArgs e)
{
checkLogin = new BLLcheckLogin();
MasterPage mp;
checkLogin.CheckLogin(Session["usertype"], IsPostBack, this);
}
我将您的代码更改为传入
页面
,而不是主页面文件
属性。Ref不再是必需的。但是我有一个类,它的函数可以更改masterpagefile。我从aspx源文件调用这个函数,但不能从PreInit之后的事件调用。太晚了。不,我有简单的业务逻辑类。这个类有一个函数,它有这些代码。那个函数是用object调用的,但从那个时候调用的事件来看,我只想知道,我怎么能知道,但我有一个类,它有一个改变masterpagefile的函数。我从aspx源文件调用这个函数,但不能从PreInit之后的事件调用。太晚了。不,我有简单的业务逻辑类。这个类有一个函数,它有这些代码。那个函数是用object调用的,但从那个时候调用的事件中,我只想知道,我怎么能知道doMasterPage是一个对象,所以它不需要通过引用传递。从方法声明和方法调用中删除“ref”以解决该错误。母版页是一个对象,因此不需要通过引用传递。从方法声明和方法调用中删除“ref”以解决该错误。