简体   繁体   English

如何在后面的代码中访问扩展 Panel 的自定义控件的公共属性?

[英]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.

相关问题 使用WPF自定义控件,如何通过后面的代码为自定义控件提供名称以访问它? - Using a WPF Custom Control, How can I give my custom control a name to access it via the code behind? 如何从后面的代码访问和设置跨度控件的属性? - How can I access and set the properties of span control from code behind? 如何从堆栈面板中的控件后面的代码访问? - How can i access from code behind, controls that are in a stack panel? 如何在基于公共属性的自定义控件中仅执行一次代码? - How can I execute code in a custom control based on public properties only once? 如何从aspx代码后面的公共类访问公共方法? - how can I access my public method from aspx code behind to a public class? 在asp.net中,要启用/禁用图像按钮,如何在asp.net代码后面的文件中访问Gridview-&gt; Itemtemplate-&gt;面板的ImageButton控件 - In asp.net, To enable/disable imagebutton, How can I access ImageButton control of Gridview->Itemtemplate->panel in asp.net code behind file 我可以从(ResourceDictionary)后面的代码访问命名控件吗? - Can I get access from code behind (of a ResourceDictionary) to a named control? 如何从代码隐藏中访问用于填充XAML类的DataContext的属性/方法? - How can I access properties/methods of the DataContext used to populate a XAML class from the code-behind? 如何在后面的代码上将自定义属性从View绑定到ViewModel? - How can I bind custom properties from View to ViewModel on code behind? 如何从 xaml 中的 ItemTemplate 访问在代码中定义的用户控件的依赖项属性? - How can I access a dependency property of a user control that was defined in code behind from a ItemTemplate in xaml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM