代码之家  ›  专栏  ›  技术社区  ›  Andreas Niedermair

在ASP.NET中创建可折叠区域

  •  0
  • Andreas Niedermair  · 技术社区  · 15 年前

    目前,我已经构建了一个collapseControl,它的行为类似于一个标签(associatedControlID),用于控制控件的折叠状态。

    我要生成以下控件:

    collapsableArea http://img692.imageshack.us/img692/3307/stackoverflowcollapseab.jpg

    我想到了这样的事情:
    把我已经构建的collapsablecontrol和其他一些控件(如panel)放在一起以获得collapsablerea。

    第一次尝试:
    我试图扩大一个小组,并做了以下工作:

    this.Parent.Controls.Add(collapsableControl);
    

    但这给了我:“生命周期步骤不正确”、“不能修改”、“空引用”…例外情况

    所以我又试了一次(我认为最好的选择,因为没有标签键):
    我扩展了一个占位符,并执行了以下操作:

    this.Controls.Add(collapsableControl);
    this.Controls.Add(collapsablePanel);
    

    这导致了其他问题,比如:我只想设置面板的文本,面板的样式,…

    有线!

    对于这种情况,您有什么解决方案吗?

    编辑:
    我想出了另一个解决办法:
    another solution http://img109.imageshack.us/img109/3307/stackoverflowcollapseab.jpg

    “collapsablerea”是“control”类型,包含2个额外的私有属性:

    1. “可折叠控件”
    2. “面板”

    我认为将collapsablerea.controls的getter重定向到collapsablerea.panel.controls就足够了。在collapsablerea.createChildControls()中,我声明并将collapsableControl和面板添加到base.controls中,并在collapsablerea.renderchildren()中呈现这些2

    我现在的问题: collapsablecontrol将获得clientID(不设置ID)-面板将不会 如果面板包含<%%>-标记,则呈现可折叠控件将失败(或传出)

    有什么建议吗?

    编辑: 我修复了丢失ID的行为-只需将collapsablecontrol.associatedcontrolID设置为panel.clientID…但是-当将<%%>放入面板时,它不会被渲染??!!

    3 回复  |  直到 15 年前
        1
  •  0
  •   Chris    15 年前

    如果您在寻找一个简单的面板,而不是重新发明轮子,您可以使用可折叠的面板控件:

    http://www.asp.net/AJAX/AjaxControlToolkit/Samples/CollapsiblePanel/CollapsiblePanel.aspx

    可能是获得所需功能的最简单方法?

        2
  •  0
  •   Andreas Niedermair    15 年前

    哦,怎么了-我已经解决了这个问题:

    public sealed class CollapsableArea : Control
    {
        private const string ViewStateKeyCollapsableContentClientID = "collapsableContentClientID";
    
        private string CollapsableContentClientID
        {
            get
            {
                var obj = this.ViewState[ViewStateKeyCollapsableContentClientID];
                if (obj == null)
                {
                    var collapsableContentClientID = Guid.NewGuid().ToString();
                    this.ViewState[ViewStateKeyCollapsableContentClientID] = collapsableContentClientID;
                    return collapsableContentClientID;
                }
                return (string)obj;
            }
        }
    
        /// <summary>
        /// Gets or sets the header text.
        /// </summary>
        /// <value>The header text.</value>
        public string HeaderText
        {
            get
            {
                this.EnsureChildControls();
                return this._collapseControl.Text;
            }
            set
            {
                this.EnsureChildControls();
                this._collapseControl.Text = value;
            }
        }
    
        public override ControlCollection Controls
        {
            get
            {
                // redirect controls
                return this._collapsableContent.Controls;
            }
        }
    
        #region child controls
    
        private readonly Panel _collapsableContent = new Panel();
        private readonly CollapsableControl _collapseControl = new CollapsableControl();
    
        #endregion
    
        public override Control FindControl(string id)
        {
            // need to redirect
            if (string.Equals(id, this._collapsableContent.ID))
            {
                return this._collapsableContent;
            }
            return this._collapsableContent.FindControl(id);
        }
    
        protected override void CreateChildControls()
        {
            base.Controls.Clear();
    
            var collapsableContentClientID = this.CollapsableContentClientID;
            this._collapsableContent.ID = collapsableContentClientID;
            this._collapseControl.AssociatedControlID = collapsableContentClientID;
            base.Controls.Add(this._collapseControl);
            base.Controls.Add(this._collapsableContent);
        }
    
        protected override void RenderChildren(HtmlTextWriter writer)
        {
            this._collapseControl.RenderControl(writer);
            // hack for code blocks
            if (!this.Controls.IsReadOnly)
            {
                this._collapsableContent.RenderControl(writer);
            }
            else
            {
                this._collapsableContent.RenderBeginTag(writer);
                base.RenderChildren(writer);
                this._collapsableContent.RenderEndTag(writer);
            }
        }
    }
    
        3
  •  -1
  •   m3kh    15 年前

    控件应获取模板控件属性以定义可折叠内容。正如您所说,一个获取标签控件ID的AssociatedControlID属性。

    public class CollapsableArea : WebControl, INamingContainer
    {
        public string AssociatedControlID { get; set; }
        public ITemplate ContentTemplate { get; set; }
    }
    

    您必须向页面的启动脚本注册jquery。

    $("#The AssociatedControlID client control id").click(function(e) {
        $("#The CollapsableArea client control id").toggle();
    }