How can I access the public properties of a custom control, which extends Panel, in code behind?
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;开发者_运维技巧
}
}
}
}
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 ... %>
<div class="cod-content-editable">
<asp:Label id="textLabel" runat="server" CssClass="Text"/>
<div class="cod-button-holder">
<asp:HyperLink id="editLink" runat="server" Text="Edit" ... />
</div>
</div>
Etc., etc. and then in your code you can wire the properties directly to the controls:
public class MyControl : UserControl
{
public string Text { get { return textLabel.Text; } set { textLabel.Text = value; } }
public Uri EditNavigateUri { get { return editLink.NavigateUri; } set { editLink.NavigateUri = value; } }
//...etc.
}
精彩评论