创建一个可折叠的区域ASP.NET
-
19-09-2019 - |
题
目前我已经建立了一个collapseControl其行为类似的标签(associatedControlID)控制的崩溃状态的控制。
以下控制我想建立:
collapsableArea http://img692.imageshack.us/img692/3307/stackoverflowcollapseab.jpg
我想是这样的:
把我已经建立collapsableControl和一些其他的控制(如:小组)一起得到一个collapsableArea.
第一次尝试:
我试图延长一个小组,并做如下:
this.Parent.Controls.Add(collapsableControl);
但这给了我"不正确的生命周期的一步","不能修改","nullReference",...例外情况
所以我给它一试(我认为更好的选择,由于没有得到tagKey):
我延长了占位,并做如下:
this.Controls.Add(collapsableControl);
this.Controls.Add(collapsablePanel);
这引起了其他问题,如:我只想要设置文本小组,该式小组...
有线!
你有任何解决方案用于这种情况?
编辑:
我来到了另一个解决方案:
另一个解决方案http://img109.imageshack.us/img109/3307/stackoverflowcollapseab.jpg
"CollapsableArea"类型"控制",包含2个额外的私人性质:
- "CollapsableControl"
- "小组"
我想这就足够了,重新定向吸气的CollapsableArea.控制CollapsableArea.小组。控制。在CollapsableArea.CreateChildControls()我instanciate和加CollapsableControl和小组基地。控制和在CollapsableArea.RenderChildren()渲染这些2
我的问题现在:该CollapsableControl会得到一个clientID(而不设置一个ID)-该小组不会 呈现CollapsableControl将失败(或传递出),如果小组包含了 <%%>-标签
任何建议?
编辑: 我固定的行为失踪ID-只是集CollapsableControl.AssociatedControlID到小组。ClientID...但是当投入 <%%>小组,它不会得到呈现的??!!
解决方案 2
哦,怎么进来-我已经解决了这个问题:
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);
}
}
}
其他提示
如果你是一个简单的小组,而不是重新发明轮子,你可以使用的可折叠小组控制:
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/CollapsiblePanel/CollapsiblePanel.aspx
可能是最简单的方式得到的功能,你是之后?
你的控制应该得到一个模板控制的财产定义可折叠的内容。和如你所说的一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();
}