Server Control using a Collection

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Server Control using a Collection

    Ok I am brand new at C#. I am attempting to build a server control that
    contains a collection. Trouble is when I place my control on my page and
    edit the collection via the property window (using an Editor) the control
    generates the following html:

    <controlgroup:ControlGroupControl id="ControlGroupControl2" runat="server"
    Items="(Collection)"></controlgroup:ControlGroupControl>

    Note that Items shows up as just "(Collections)". Now the desired tag
    structure is similar to the following:

    <asp:DropDownList id="DropDownList1" runat="server">
    <asp:ListItem Value="3333">3333</asp:ListItem>
    <asp:ListItem Value="ffffffffff">ffffffff</asp:ListItem>
    </asp:DropDownList>

    So now how would one go about creating a control to automatically generate
    the correct tags and also read from them as well?

    Thanks,

    Matt



    ME Guest

  2. Similar Questions and Discussions

    1. Collection Property of Another Custom Server Control
      Hi, I read more and figured out how to build what I needed: <cc> <item></item> <item></item> </cc> when <item> is an editable control in...
    2. server control with collection
      Hello, I wrote a simple server control from a sample I found on the web in the following URL: http://west-wind.com/weblog/posts/200.aspx The...
    3. server control collection with several types of properties
      Is it possible to implement a server control that will look like this: <just:control> <columns> <columnTypeA id=1></columnTypeA> <columnTypeA...
    4. Web Form Designer deleting collection markup on Custom Server Control
      I am trying to implement a custom server control which exposes a collection and uses the ParseChildrenAttribute to populate it from child elements...
    5. Server Control Collection Properties Solutions,Problems MVP Advice Requested
      I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the problems that I have encountered...
  3. #2

    Default Server Control using a Collection

    Ok I am brand new at C#. I am attempting to build a server control that
    contains a collection. Trouble is when I place my control on my page and
    edit the collection via the property window (using an Editor) the control
    generates the following html:

    <controlgroup:ControlGroupControl id="ControlGroupControl2" runat="server"
    Items="(Collection)"></controlgroup:ControlGroupControl>

    Note that Items shows up as just "(Collections)". Now the desired tag
    structure is similar to the following:

    <asp:DropDownList id="DropDownList1" runat="server">
    <asp:ListItem Value="3333">3333</asp:ListItem>
    <asp:ListItem Value="ffffffffff">ffffffff</asp:ListItem>
    </asp:DropDownList>

    So now how would one go about creating a control to automatically generate
    the correct tags and also read from them as well?

    Thanks,

    Matt



    ME Guest

  4. #3

    Default Server Control using a Collection

    Ok I am brand new at C#. I am attempting to build a server control that
    contains a collection. Trouble is when I place my control on my page and
    edit the collection via the property window (using an Editor) the control
    generates the following html:

    <controlgroup:ControlGroupControl id="ControlGroupControl2" runat="server"
    Items="(Collection)"></controlgroup:ControlGroupControl>

    Note that Items shows up as just "(Collections)". Now the desired tag
    structure is similar to the following:

    <asp:DropDownList id="DropDownList1" runat="server">
    <asp:ListItem Value="3333">3333</asp:ListItem>
    <asp:ListItem Value="ffffffffff">ffffffff</asp:ListItem>
    </asp:DropDownList>

    So now how would one go about creating a control to automatically generate
    the correct tags and also read from them as well?

    Thanks,

    Matt



    ME Guest

  5. #4

    Default Server Control using a Collection

    Ok I am brand new at C#. I am attempting to build a server control that
    contains a collection. Trouble is when I place my control on my page and
    edit the collection via the property window (using an Editor) the control
    generates the following html:

    <controlgroup:ControlGroupControl id="ControlGroupControl2" runat="server"
    Items="(Collection)"></controlgroup:ControlGroupControl>

    Note that Items shows up as just "(Collections)". Now the desired tag
    structure is similar to the following:

    <asp:DropDownList id="DropDownList1" runat="server">
    <asp:ListItem Value="3333">3333</asp:ListItem>
    <asp:ListItem Value="ffffffffff">ffffffff</asp:ListItem>
    </asp:DropDownList>

    So now how would one go about creating a control to automatically generate
    the correct tags and also read from them as well?

    Thanks,

    Matt




    ME Guest

  6. #5

    Default Re: Server Control using a Collection

    Hi,

    1) the collection property needs to be read-only (instantiate the collection
    when property is accessed for the first time)
    2). collection property should be declared with
    PersistenceMode(PersistenceMode.InnerProperty),
    DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content) and
    NotifyParentProperty(true) attributes

    Have you done these? If this doesn't help, post the code.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    [url]http://blogs.aspadvice.com/joteke[/url]



    "ME" <trash.trash@comcastDOTnet> wrote in message
    news:3sOEc.7440$MB3.867@attbi_s04...
    > Ok I am brand new at C#. I am attempting to build a server control that
    > contains a collection. Trouble is when I place my control on my page and
    > edit the collection via the property window (using an Editor) the control
    > generates the following html:
    >
    > <controlgroup:ControlGroupControl id="ControlGroupControl2" runat="server"
    > Items="(Collection)"></controlgroup:ControlGroupControl>
    >
    > Note that Items shows up as just "(Collections)". Now the desired tag
    > structure is similar to the following:
    >
    > <asp:DropDownList id="DropDownList1" runat="server">
    > <asp:ListItem Value="3333">3333</asp:ListItem>
    > <asp:ListItem Value="ffffffffff">ffffffff</asp:ListItem>
    > </asp:DropDownList>
    >
    > So now how would one go about creating a control to automatically generate
    > the correct tags and also read from them as well?
    >
    > Thanks,
    >
    > Matt
    >
    >
    >

    Teemu Keiski Guest

  7. #6

    Default Re: Server Control using a Collection

    Thank you. What would it take to get the control to "read" these values
    after the GUI page has been saved and closed then reopened again? It seems
    that if I do this the control reverts to "Error Creating Control". My guess
    is that it is not reading these values but I am not absolutly certain.
    Below is my controls code (please excuse my ignorance, like I said I am very
    new to this!):


    using System;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.Design;

    using System.ComponentModel;

    using System.ComponentModel.Design;

    using System.Collections;

    using System.IO;

    using System.Text;

    using System.Drawing.Design;

    //using System.

    [

    assembly: TagPrefix("ControlGroup","ControlGroup")

    ]

    namespace ControlGroup

    {

    /// <summary>

    /// Summary description for WebCustomControl1.

    /// </summary>

    ///

    [DefaultProperty("Text"),

    ToolboxData("<{0}:ControlGroupControl
    runat=server></{0}:ControlGroupControl>"),

    SerializableAttribute()]

    public class ControlGroupControl : System.Web.UI.WebControls.WebControl,
    INamingContainer

    {

    #region Private Variables

    private string text="";

    private ControlGroupItemCollection items = new ControlGroupItemCollection();

    private ControlGroupItemCollection allItems = new
    ControlGroupItemCollection();

    private ListBox lbItems = new ListBox();

    private Label lblTitleID = new Label();

    private TextBox txtInfo = new TextBox();

    #endregion

    #region Constructors and Initializers

    public ControlGroupControl()

    {

    try

    {

    this.Visible=true;

    txtInfo.Height=300;

    //this.Height=100;

    //this.Width=100;

    lbItems.BorderStyle=BorderStyle.None;

    lbItems.BorderWidth=0;

    lbItems.Width=300;

    txtInfo.Width=300;

    txtInfo.TextMode=TextBoxMode.MultiLine;

    txtInfo.Text += "CONSTRUCTED!\n";

    }

    catch (Exception ex)

    {

    items=null;

    allItems=null;

    text="Error! - " + ex.ToString();

    }

    }

    protected override void OnInit(EventArgs e)

    {

    if (AllItems.Count != Page.Controls.Count-1)

    {

    txtInfo.Text += "INITIALIZED & RESET!\n";

    fillAllItems();

    }

    base.OnInit (e);

    }

    protected override void OnLoad(EventArgs e)

    {

    txtInfo.Text += "LOADED!\n";

    base.OnLoad (e);

    }

    protected override void OnPreRender(EventArgs e)

    {

    txtInfo.Text += "PRE-RENDER!\n";

    base.OnPreRender (e);

    }



    #endregion

    #region Void Methods

    private void FillListBox()

    {

    //Start by clearing the list

    lbItems.Items.Clear();

    //If the items collection has not been instantiated then

    //Do not attempt to iterate through it.

    if (items.Count>0)

    {

    //for (int i=0; i< items.Count; i++)

    foreach(ControlGroupItem cntl in items)

    {

    lbItems.Items.Add(cntl.ControlID.ToString());

    }

    }

    else

    {

    //Writes "No Controls

    lbItems.Items.Add("No Controls");

    }



    }

    private void fillAllItems()

    {

    try

    {

    int c = Page.Controls.Count;

    int tmpID=0;

    ControlGroupItem tempControl = new ControlGroupItem();

    allItems.Clear();


    for(int i=0; i < c; i++)

    {

    //txtInfo.Text = txtInfo.Text + Page.Controls[i].ID.ToString() + "\n";

    tempControl = new ControlGroupItem();

    // set the control structure properties of the control

    tempControl.ControlID = Page.Controls[i].ID.ToString();

    tempControl.ControlType = Page.Controls[i].GetType();

    //tempControl.BoundProperty=

    // add the control structure to the collection

    tmpID=this.allItems.Add(tempControl);

    txtInfo.Text +="CONTROL " + this.allItems[tmpID].ControlID.ToString() + " ["
    + allItems.Count.ToString() + "]\n";

    }

    this.Text="WORKED!";

    }

    catch (Exception ex)

    {

    System.Diagnostics.Debug.Write(ex.ToString());

    this.Text=ex.ToString();

    }

    }

    #endregion


    #region Properties

    [

    Bindable(true),

    Category("Data"),

    ]

    public string Text

    {

    get

    {

    return text;

    }

    set

    {

    text=value;

    }

    }



    /// <summary>

    /// Items = The group of referenced controls

    /// </summary>

    [Category("Misc"),

    PersistenceMode(PersistenceMode.InnerProperty),

    DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content),

    NotifyParentProperty(true),

    EditorAttribute(typeof(ControlGroupEditor), typeof(UITypeEditor))

    ]

    public ControlGroupItemCollection Items

    {

    get

    {

    return items;

    }

    }

    [BrowsableAttribute (false)]

    public ControlGroupItemCollection AllItems

    {

    get

    {

    return allItems;

    }

    set

    {

    allItems = value;

    }

    }

    #endregion

    #region Rendering

    protected override StateBag ViewState

    {

    get

    {

    return base.ViewState;

    }

    }

    protected override object SaveViewState()

    {

    return base.SaveViewState ();

    }

    protected override void LoadViewState(object savedState)

    {

    base.LoadViewState (savedState);

    }



    protected override void RenderContents(HtmlTextWriter writer)

    {

    //Render(writer);

    base.RenderContents (writer);

    }



    protected override void RenderChildren(HtmlTextWriter writer)

    {

    FillListBox();

    lblTitleID.Text=this.ID.ToString();

    // Render

    lblTitleID.RenderControl(writer);

    writer.Write("<BR>");

    lbItems.RenderControl(writer);

    writer.Write("<BR>");

    txtInfo.RenderControl(writer);

    }

    #endregion

    }

    }







    "Teemu Keiski" <joteke@aspalliance.com> wrote in message
    news:OT9QxF4XEHA.1356@TK2MSFTNGP09.phx.gbl...
    > Hi,
    >
    > 1) the collection property needs to be read-only (instantiate the
    collection
    > when property is accessed for the first time)
    > 2). collection property should be declared with
    > PersistenceMode(PersistenceMode.InnerProperty),
    > DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)
    and
    > NotifyParentProperty(true) attributes
    >
    > Have you done these? If this doesn't help, post the code.
    >
    > --
    > Teemu Keiski
    > MCP, Microsoft MVP (ASP.NET), AspInsiders member
    > ASP.NET Forum Moderator, AspAlliance Columnist
    > [url]http://blogs.aspadvice.com/joteke[/url]
    >
    >
    >
    > "ME" <trash.trash@comcastDOTnet> wrote in message
    > news:3sOEc.7440$MB3.867@attbi_s04...
    > > Ok I am brand new at C#. I am attempting to build a server control that
    > > contains a collection. Trouble is when I place my control on my page
    and
    > > edit the collection via the property window (using an Editor) the
    control
    > > generates the following html:
    > >
    > > <controlgroup:ControlGroupControl id="ControlGroupControl2"
    runat="server"
    > > Items="(Collection)"></controlgroup:ControlGroupControl>
    > >
    > > Note that Items shows up as just "(Collections)". Now the desired tag
    > > structure is similar to the following:
    > >
    > > <asp:DropDownList id="DropDownList1" runat="server">
    > > <asp:ListItem Value="3333">3333</asp:ListItem>
    > > <asp:ListItem Value="ffffffffff">ffffffff</asp:ListItem>
    > > </asp:DropDownList>
    > >
    > > So now how would one go about creating a control to automatically
    generate
    > > the correct tags and also read from them as well?
    > >
    > > Thanks,
    > >
    > > Matt
    > >
    > >
    > >
    >
    >

    ME Guest

  8. #7

    Default Re: Server Control using a Collection

    Hi,

    this might help :

    [url]http://weblogs.asp.net/ngur/articles/144770.aspx[/url]

    Natty Gur[MVP]

    blog : [url]http://weblogs.asp.net/ngur[/url]
    Mobile: +972-(0)52-8888377


    *** Sent via Devdex [url]http://www.devdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139