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