3
我想创建一个继承自Web控件“面板”的自定义控件。控件本身就像一个常规的Panel控件一样工作,但是我只能直接在.aspx页面上的控件上设置公共属性。我一直试图在Page_Load期间的代码中修改它们,但显然在页面生命周期中太晚了。当我尝试尽早做到这一点时,我得到一个空引用错误。有没有什么我可以在自定义控件中完成,以便在Page_Load()期间可以修改它?如何在后台代码中访问扩展Panel的自定义控件的公共属性?
在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;
}
}
}
}
这取决于当你正在访问控件中的属性 - 请发布一些代码,以便我们看到你是如何做到这一点的。 – 2011-05-03 17:59:34
in aspx page,this is the control with properties set(this works fine):
这是面板中的一些文字
cod:ContentOnDemandPanel> – 2011-05-03 18:29:00在代码后面(我试过生命周期的其他部分)。这是行不通的。 protected void Page_Load(object sender,EventArgs e) codPanelLocation.LocationId = 22; codPanelOfferingText.TelerikToolTipSkinName =“Black”; } – 2011-05-03 18:30:45