How can I access the public properties of a custom control that extends Panel in code?

I am trying to create a custom control that inherits from the Panel web control. The control itself works the way it acts like a regular panel control, however I can only set public properties directly in the control on the .aspx page. I am trying to change them in the code standing at the time of Page_Load, but it seems too late in the page life cycle. When I try to do this earlier, I get a null link error. Is there something that I can do in the custom control itself so that it can be changed during Page_Load ()?

In ASPX, this is a control with a set of properties (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 below (and I 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 things that are probably not 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;
        }
    }
}

}

+3
source share
1 answer

I would suggest, based on your code above, that you will draw your control to another level. Create an ASCX that contains the label (to save the text "T") and two hyperlinks for adding / editing.

<% @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 associate properties directly with 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.
}
0
source

All Articles