0

現在、コントロールの折りたたみ状態を制御するために、ラベル(associatedControlID)と同様に動作するcollapseControlを作成しました。

次のコントロールを構築したい:

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

私は次のようなことを考えました:
collapsableAreaを取得するために、すでにビルドしたcollapsableControlと他のコントロール(パネルなど)を組み合わせます。

最初の試み:
私はパネルを拡張しようとし、次のことを行いました:

this.Parent.Controls.Add(collapsableControl);

しかし、これは私に与えました:「ライフサイクルステップが正しくない」、「変更できない」、「nullReference」、...例外

だから私はそれをもう一度試してみました(tagKeyがないため、より良い選択だと思います):
プレースホルダーを拡張して次のことを行いました:

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

これにより、次のような他の問題が発生しました。パネルのテキスト、パネルのスタイルのみを設定したい...

有線!

このシナリオの解決策はありますか?

編集:
私は別の解決策を思いついた:
別の解決策http://img109.imageshack.us/img109/3307/stackoverflowcollapseab.jpg

「CollapsableArea」は「Control」タイプで、2つの追加のプライベートプロパティが含まれています。

  1. 「CollapsableControl」
  2. "パネル"

CollapsableArea.ControlsのゲッターをCollapsableArea.Panel.Controlsにリダイレクトするだけで十分だと思いました。CollapsableArea.CreateChildControls()で、CollapsableControlとPanelをインスタンス化してbase.Controlsに追加し、CollapsableArea.RenderChildren()でそれらをレンダリングします2

今の私の問題:CollapsableControlは(IDを設定せずに)clientIDを取得します-パネルに<%%>タグが含まれている場合、パネルはCollapsableControlをレンダリングしません(または渡されます)

助言がありますか?

編集: 欠落しているIDの動作を修正しました-CollapsableControl.AssociatedControlIDをPanel.ClientIDに設定するだけです...しかし-パネルに<%%>を配置すると、レンダリングされませんか?? !!

4

3 に答える 3

0

単純なパネルを探している場合は、ホイールを再発明するのではなく、折りたたみ式パネルコントロールを使用できます。

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

あなたが求めている機能を手に入れる最も簡単な方法でしょうか?

于 2009-11-11T10:12:19.983 に答える
0

ああ、どうして-私はこの問題を解決しました:

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);
        }
    }
}
于 2009-11-13T09:38:17.100 に答える
-1

Your control should get a Template control property to define collapsable content. And as you said a AssociatedControlID property which gets the Label control id.

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

You have to register a jquery to the startup scripts of the page.

$("#The AssociatedControlID client control id").click(function(e) {
    $("#The CollapsableArea client control id").toggle();
}
于 2009-11-11T10:37:17.420 に答える