如何在代码隐藏中访问扩展面板的自定义控件的公共属性?
我正在尝试创建一个继承自 Web 控件“Panel”的自定义控件。该控件本身的工作方式与常规面板控件类似,但我只能直接在 .aspx 页面上的控件中设置其公共属性。我一直在尝试在 Page_Load 期间的代码中修改它们,但这显然在页面生命周期中为时已晚。当我尝试更早地执行此操作时,我收到空引用错误。我可以在自定义控件本身中执行某些操作,以便可以在 Page_Load() 期间对其进行修改吗?
在 ASPX 中,这是设置了属性的控件(这工作正常):
<cod:ContentOnDemandPanel LocationId="4" ID="codPanelLocation" runat="server">
<p>This is some text that is in the panel</p>
</cod:ContentOnDemandPanel>
尽管在后面的代码中(并且我已经尝试了生命周期的其他部分)。这不起作用。
protected void Page_Load(object sender, EventArgs e)
{
codPanelLocation.LocationId = 22;
codPanelOfferingText.TelerikToolTipSkinName = "Black";
}
任何帮助表示赞赏。
这是我的类(删除了一些可能不供公众使用的内容)
命名空间 Home.ContentOnDemand {
[ToolboxData("<{0}:ContentOnDemandPanel runat=server></{0}:ContentOnDemandPanel>")]
public partial class ContentOnDemandPanel : Panel, INamingContainer
{
private ContentOnDemandDataSource _contentDataSource;
private RadToolTip _contentEditTooltip;
private ContentOnDemandItem _contentItem;
private int _contentItemId;
private bool _isEditMode;
private bool _isSharedContent;
private string _telerikToolTipSkinName;
private MasterWebDatabaseDataType _mwdbDataType;
private string _mwdbDataKeyValue;
private int _locationId;
private int _programId;
private int _areaOfStudyId;
private int _programOfferingId;
public ContentOnDemandPanel()
{
}
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
protected override void CreateChildControls()
{
if (_isEditMode == false)
{
this._isEditMode = CODUtilities.isUserInEditMode();
}
if (this._isEditMode)
{
Controls.Clear();
if (_contentItemId > 0 && _contentDataSource == ContentOnDemandDataSource.CommonContent)
{
_contentItem = GetCommonContentItemByContentId(this._contentItemId);
if (_contentItem != null)
{
_contentEditTooltip = this.BuildRadToolTip();
Controls.Add(_contentEditTooltip);
}
}
else
{
if (_contentDataSource == ContentOnDemandDataSource.MasterWeb)
{
_contentItem = GetCommonContentItemForMasterWeb(_mwdbDataType);
_contentEditTooltip = this.BuildRadToolTip();
Controls.Add(_contentEditTooltip);
}
}
}
}
public void AddButtonsToToolTip(RadToolTip tooltip, ContentOnDemandItem item)
{
if (item != null)
{
if ((item != null) && ((item.EditItemPath.Length > 5) || (item.NewItemPath.Length > 5)))
{
Panel pnlButtonHolder = new Panel
{
CssClass = "cod-button-holder"
};
HyperLink editButton = this.BuildEditButton(item.EditItemPath);
HyperLink addNewButton = this.BuildNewRecordButton(item.NewItemPath);
if (editButton != null)
{
pnlButtonHolder.Controls.Add(editButton);
}
if (addNewButton != null)
{
pnlButtonHolder.Controls.Add(addNewButton);
}
tooltip.Controls.AddAt(0, pnlButtonHolder);
this.CssClass = "cod-content-editable";
}
else
{
this.makePanelNotCOD();
}
}
else
{
this.makePanelNotCOD();
}
}
private HyperLink BuildEditButton(string editPath)
{
if (editPath.Length < 5)
{
return null;
}
return new HyperLink { CssClass = "cod-button-edit", Text = "Edit Record", NavigateUrl = editPath, Target = "CMSWindow" };
}
private HyperLink BuildNewRecordButton(string newRecordPath)
{
if (newRecordPath.Length < 5)
{
return null;
}
return new HyperLink { CssClass = "cod-button-new", Text = "Add New Record", NavigateUrl = newRecordPath, Target = "CMSWindow" };
}
private RadToolTip BuildRadToolTip()
{
RadToolTip tt = new RadToolTip();
AddButtonsToToolTip(tt,_contentItem);
return tt;
}
private ContentOnDemandItem GetCommonContentItemForMasterWeb(MasterWebDatabaseDataType dataType)
{
ContentOnDemandItem item = new ContentOnDemandItem();
return item;
}
public static ContentOnDemandItem GetCommonContentItemByContentId(int contentId)
{
ContentOnDemandItem item = new ContentOnDemandItem();
return item;
}
private void makePanelNotCOD()
{
this._isEditMode = false;
this.CssClass = "";
this._contentEditTooltip = null;
}
protected override void Render(HtmlTextWriter writer)
{
this.RenderBeginTag(writer);
foreach (Control c in base.Controls) if (!c.Equals(_contentEditTooltip))
c.RenderControl(writer);
if (_contentEditTooltip != null)
{
this._contentEditTooltip.RenderControl(writer);
}
this.RenderEndTag(writer);
}
// Properties
public ContentOnDemandDataSource ContentDataSource
{
get
{
return this._contentDataSource;
}
set
{
this._contentDataSource = value;
}
}
public ContentOnDemandItem ContentItem
{
get
{
return this._contentItem;
}
set
{
this._contentItem = value;
}
}
public bool IsEditMode
{
get { return _isEditMode; }
set { _isEditMode = value; }
}
public int ContentItemId
{
get
{
return this._contentItemId;
}
set
{
this._contentItemId = value;
}
}
public bool IsSharedContent
{
get
{
return this._isSharedContent;
}
set
{
this._isSharedContent = value;
}
}
public int ProgramId
{
get { return _programId; }
set { _programId = value; }
}
public int AreaOfStudyId
{
get { return _areaOfStudyId; }
set { _areaOfStudyId = value; }
}
public int ProgramOfferingId
{
get { return _programOfferingId; }
set { _programOfferingId = value; }
}
public MasterWebDatabaseDataType MwdbDataType
{
get { return _mwdbDataType; }
set { _mwdbDataType = value; }
}
public string MwdbDataKeyValue
{
get { return _mwdbDataKeyValue; }
set { _mwdbDataKeyValue = value; }
}
public int LocationId
{
get { return _locationId; }
set { _locationId = value; }
}
public string TelerikToolTipSkinName
{
get
{
return this._telerikToolTipSkinName;
}
set
{
this._telerikToolTipSkinName = value;
}
}
}
}
I am trying to create a custom control that inherits from the web control "Panel". The control itself is working as it acts like a regular Panel control, however I am only able to set the public properties on it directly in the control on the .aspx page. I have been trying to modify them in the code behind during Page_Load but that apparently is too late in the page life-cycle. When I try to do it earlier though, I get a null reference error. Is there something that I can do in the custom control itself so that it can be modified during Page_Load() ?
In the ASPX, this is the control with properties set (this works fine):
<cod:ContentOnDemandPanel LocationId="4" ID="codPanelLocation" runat="server">
<p>This is some text that is in the panel</p>
</cod:ContentOnDemandPanel>
In the code behind though (and I've tried other parts of the life-cycle). This does NOT work.
protected void Page_Load(object sender, EventArgs e)
{
codPanelLocation.LocationId = 22;
codPanelOfferingText.TelerikToolTipSkinName = "Black";
}
Any help is appreciated.
Here is my class (removed some stuff that probably isn't for public consumption)
namespace Home.ContentOnDemand
{
[ToolboxData("<{0}:ContentOnDemandPanel runat=server></{0}:ContentOnDemandPanel>")]
public partial class ContentOnDemandPanel : Panel, INamingContainer
{
private ContentOnDemandDataSource _contentDataSource;
private RadToolTip _contentEditTooltip;
private ContentOnDemandItem _contentItem;
private int _contentItemId;
private bool _isEditMode;
private bool _isSharedContent;
private string _telerikToolTipSkinName;
private MasterWebDatabaseDataType _mwdbDataType;
private string _mwdbDataKeyValue;
private int _locationId;
private int _programId;
private int _areaOfStudyId;
private int _programOfferingId;
public ContentOnDemandPanel()
{
}
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
protected override void CreateChildControls()
{
if (_isEditMode == false)
{
this._isEditMode = CODUtilities.isUserInEditMode();
}
if (this._isEditMode)
{
Controls.Clear();
if (_contentItemId > 0 && _contentDataSource == ContentOnDemandDataSource.CommonContent)
{
_contentItem = GetCommonContentItemByContentId(this._contentItemId);
if (_contentItem != null)
{
_contentEditTooltip = this.BuildRadToolTip();
Controls.Add(_contentEditTooltip);
}
}
else
{
if (_contentDataSource == ContentOnDemandDataSource.MasterWeb)
{
_contentItem = GetCommonContentItemForMasterWeb(_mwdbDataType);
_contentEditTooltip = this.BuildRadToolTip();
Controls.Add(_contentEditTooltip);
}
}
}
}
public void AddButtonsToToolTip(RadToolTip tooltip, ContentOnDemandItem item)
{
if (item != null)
{
if ((item != null) && ((item.EditItemPath.Length > 5) || (item.NewItemPath.Length > 5)))
{
Panel pnlButtonHolder = new Panel
{
CssClass = "cod-button-holder"
};
HyperLink editButton = this.BuildEditButton(item.EditItemPath);
HyperLink addNewButton = this.BuildNewRecordButton(item.NewItemPath);
if (editButton != null)
{
pnlButtonHolder.Controls.Add(editButton);
}
if (addNewButton != null)
{
pnlButtonHolder.Controls.Add(addNewButton);
}
tooltip.Controls.AddAt(0, pnlButtonHolder);
this.CssClass = "cod-content-editable";
}
else
{
this.makePanelNotCOD();
}
}
else
{
this.makePanelNotCOD();
}
}
private HyperLink BuildEditButton(string editPath)
{
if (editPath.Length < 5)
{
return null;
}
return new HyperLink { CssClass = "cod-button-edit", Text = "Edit Record", NavigateUrl = editPath, Target = "CMSWindow" };
}
private HyperLink BuildNewRecordButton(string newRecordPath)
{
if (newRecordPath.Length < 5)
{
return null;
}
return new HyperLink { CssClass = "cod-button-new", Text = "Add New Record", NavigateUrl = newRecordPath, Target = "CMSWindow" };
}
private RadToolTip BuildRadToolTip()
{
RadToolTip tt = new RadToolTip();
AddButtonsToToolTip(tt,_contentItem);
return tt;
}
private ContentOnDemandItem GetCommonContentItemForMasterWeb(MasterWebDatabaseDataType dataType)
{
ContentOnDemandItem item = new ContentOnDemandItem();
return item;
}
public static ContentOnDemandItem GetCommonContentItemByContentId(int contentId)
{
ContentOnDemandItem item = new ContentOnDemandItem();
return item;
}
private void makePanelNotCOD()
{
this._isEditMode = false;
this.CssClass = "";
this._contentEditTooltip = null;
}
protected override void Render(HtmlTextWriter writer)
{
this.RenderBeginTag(writer);
foreach (Control c in base.Controls) if (!c.Equals(_contentEditTooltip))
c.RenderControl(writer);
if (_contentEditTooltip != null)
{
this._contentEditTooltip.RenderControl(writer);
}
this.RenderEndTag(writer);
}
// Properties
public ContentOnDemandDataSource ContentDataSource
{
get
{
return this._contentDataSource;
}
set
{
this._contentDataSource = value;
}
}
public ContentOnDemandItem ContentItem
{
get
{
return this._contentItem;
}
set
{
this._contentItem = value;
}
}
public bool IsEditMode
{
get { return _isEditMode; }
set { _isEditMode = value; }
}
public int ContentItemId
{
get
{
return this._contentItemId;
}
set
{
this._contentItemId = value;
}
}
public bool IsSharedContent
{
get
{
return this._isSharedContent;
}
set
{
this._isSharedContent = value;
}
}
public int ProgramId
{
get { return _programId; }
set { _programId = value; }
}
public int AreaOfStudyId
{
get { return _areaOfStudyId; }
set { _areaOfStudyId = value; }
}
public int ProgramOfferingId
{
get { return _programOfferingId; }
set { _programOfferingId = value; }
}
public MasterWebDatabaseDataType MwdbDataType
{
get { return _mwdbDataType; }
set { _mwdbDataType = value; }
}
public string MwdbDataKeyValue
{
get { return _mwdbDataKeyValue; }
set { _mwdbDataKeyValue = value; }
}
public int LocationId
{
get { return _locationId; }
set { _locationId = value; }
}
public string TelerikToolTipSkinName
{
get
{
return this._telerikToolTipSkinName;
}
set
{
this._telerikToolTipSkinName = value;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议,根据上面的代码,您将控制抽象到另一个级别。创建一个 ASCX,其中包含一个标签(用于保存“T”文本)和两个用于添加/编辑的超链接。
<%@控制...%>
等等,然后在代码中您可以将属性直接连接到控件:
I'd suggest, based on your code above, that you abstract your control out another level. Create an ASCX which contains a Label (to hold the "T" text) and the two hyperlinks for add/edit.
<%@ Control ... %>
Etc., etc. and then in your code you can wire the properties directly to the controls: