[英]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".我正在尝试创建一个继承自 web 控件“面板”的自定义控件。 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.该控件本身就像一个常规的 Panel 控件一样工作,但是我只能在 .aspx 页面上的控件中直接设置它的公共属性。 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.我一直试图在 Page_Load 期间在后面的代码中修改它们,但这显然在页面生命周期中为时已晚。 When I try to do it earlier though, I get a null reference error.但是,当我尝试早点执行此操作时,我收到 null 参考错误。 Is there something that I can do in the custom control itself so that it can be modified during Page_Load()?我可以在自定义控件本身中做些什么,以便可以在 Page_Load() 期间对其进行修改?
In the ASPX, this is the control with properties set (this works fine):在 ASPX 中,这是设置了属性的控件(工作正常):
<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)这是我的 class (删除了一些可能不适合公众消费的东西)
namespace Home.ContentOnDemand {命名空间 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.创建一个包含 Label(保存“T”文本)和两个用于添加/编辑的超链接的 ASCX。
<%@ 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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.