locked
Extending Multiview RRS feed

  • Question

  • User189012047 posted

    Ok, I've looked around for this answer and can't really come up with anything. Sorry in advance as this is going to seem petty, but it's driving me absolutely insane.

    I'm trying to extend the Multiview so that Views can be selected by a Key rather than an Index.  So if I have a MultiView with two Views I could set a property, say ActiveViewKey to a descriptive name rather than a number.  I'd just use the ID property of the View, but I don't want to enforce that these keys be unique page wide, only unique within that MultiView.  So the ideal markup would be something like:

    <asp:KeyedMultiView ID="MyKMV" runat="server">
    <KeyedView Key="TheButton">
    <asp:Button ID="MyButton" Text="Click Me!" runat="server" />
    </KeyedView>

    <KeyedView Key="ConfirmationMsg">
    Congratulations! You clicked the button!
    </KeyedView>
    </asp:KeyedMultiView>

    And then of course you could activate the confirmation View with MyKMV.ActiveViewKey = "ConfirmationMsg".

    I tried extending MultiView and View, but two things are plaging me.  First the contents of my Views are always empty, and second, the editor still requires runat="server" on each KeyedView.  The code I'm using is as follows:

    // I've tried with and without PersistChildren(true/false) here
    [ParseChildren(typeof(KeyedView), DefaultProperty="Views")]
        public class KeyedMultiView : MultiView
        {
            protected string activeViewKey = "";
            public string ActiveViewKey {
                get { return activeViewKey; }
                set {
                    KeyedView kv = (Views as KeyedViewCollection)[activeViewKey];
                    if (kv != null)
                    {
                        activeViewKey = value;
                        ActiveViewIndex = Views.IndexOf(kv);
                    }
                }
            }

            protected KeyedViewCollection views = null;

            [PersistenceMode(PersistenceMode.InnerDefaultProperty), Browsable(false)]
            public override ViewCollection Views
            {
                get {
                    if (views == null) views = new KeyedViewCollection(this);
                    return views;
                }
            }


            public class KeyedViewCollection : ViewCollection
            {
                public KeyedViewCollection(Control owner) : base(owner)
                {
                }
            
                public override void AddAt(int index, Control v)
                {
                    if (v is KeyedView)
                         base.AddAt(index, v);
                }

                public KeyedView this[string key] {
                    get {
                        foreach (KeyedView kv in this)
                            if (kv.Key == key) return kv;
                            
                        return null;
                    }
                }
            }
        }


        [ParseChildren(false)]
        public class KeyedView : View
        {
            public string Key { get; set; }
        }

    Any help would be appreciated. Thanks in advance.

    Wednesday, September 23, 2009 12:39 PM

Answers

  • User189012047 posted

    Ok, I was under the impression you didn't need runat="server" in the View control.  I see that both of my issues were related :)  After adding runat to each KeyedView and setting ActiveViewKey in the KeyedMultiView tag, all worked as expected.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 23, 2009 1:00 PM