我正在尝试将ASP.net Web应用程序转换为网站。我遇到的一个问题是试图访问我的代码文件中的用户控件和母版页。
下面的代码显示了我用于基类的类,它继承自System.Web.UI.Page。
public class AdminBase : BasePage
{
DeniedAccess deniedAccessControl;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//All pages that inherit from this will have a denied access control which will display when an admin of one instance tries to access admin pages of a different instance
//which they don't have access to. This enables us to still use the built in web.config authroisation API
deniedAccessControl = (DeniedAccess)Page.LoadControl("~/App_Controls/DeniedAccess.ascx");
((MasterPage1)Master).MainContent.Controls.Add(deniedAccessControl);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (deniedAccessControl.ContentVisible)
{
foreach (Control control in ((MasterPage1)Master).MainContent.Controls)
{
//only sets to false if the control is set to true as it will error if you try to set a non visiual controls "Visible" property. E.g an sqldatasource control
if (control.GetType() != deniedAccessControl.GetType() && control.Visible)
control.Visible = false;
}
}
}
}
如果用户未通过某些自定义授权,则“ DeniedAccess”控件是我正在动态添加的用户控件。
当这是一个Web应用程序时,这工作得很好,因为DeniedAccess控件被编译到同一个dll中,因此我可以从代码中访问它。现在它是一个网站,因为它不在App_Code文件夹中,所以它找不到名称空间/类。
我正在网站项目中尝试做的事情吗?我似乎找不到办法,我看到的唯一替代方法是必须为每个单独的页面编写此代码,而不是在基础页面上使用它。
当出于相同原因试图将母版页投射到“ MasterPage1”类时,我也遇到相同的问题,它找不到它,所以我无法访问它的属性。
我最终创建了两个接口来解决此问题,因为它不是文件后面的代码,我不能只在源代码中添加对控件/母版页的引用。
我让我的usercontrol继承了这个界面
public interface IDeniedAccessControl
{
bool ContentVisible
{
get;
}
}
我的母版页继承了这一页
public interface IMasterPage
{
void AddControl(Control control);
ControlCollection MainContentControls
{
get;
}
}
然后,使用户控件和母版页在其中实现具有所需功能的相应界面。
母版页实现:
public void AddControl(Control control)
{
ContentPlaceHolder1.Controls.Add(control);
}
public ControlCollection MainContentControls
{
get { return ContentPlaceHolder1.Controls; }
}
用户控件的实现:
bool IDeniedAccessControl.ContentVisible
{
get { return ContentASPxPanel.Visible; }
}
然后,我可以在任何代码文件上使用它。以下是我如何编辑原始代码。
public class AdminBase : BasePage
{
IDeniedAccessControl deniedAccessControl;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//All pages that inherit from this will have a denied access control which will display when an admin of one instance tries to access admin pages of a different instance
//which they don't have access to. This enables us to still use the built in web.config authroisation API
deniedAccessControl = (IDeniedAccessControl)Page.LoadControl("~/App_Controls/DeniedAccess.ascx");
((IMasterPage)Master).AddControl((Control)deniedAccessControl);
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (deniedAccessControl.ContentVisible)
{
foreach (Control control in ((IMasterPage)Master).MainContentControls)
{
//only sets to false if the control is set to true as it will error if you try to set a non visiual controls "Visible" property. E.g an sqldatasource control
if (control.GetType() != deniedAccessControl.GetType() && control.Visible)
control.Visible = false;
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句