How to expose collections on a usercontrol

J

j_a5

Hi,
I have a custom user control. In that custom control MyControl. I am
exposing a property called MyItems. The MyItems is a
strongly typed collection of MyItem. I am exposing a MyItems in the user
control as a public property. I want to be able to add items to the MyItems
collection at design time, i.e., I want to specify something like this in my
aspx page.

<uc1:MyCustomControl1 id="MyCustomControl1" runat="server>
<MyItems>
<MyItem Text = "text">
<MyItem Text = "text2">
</MyItems>
</uc1:MyCustomControl1>

Description: An error occurred during the compilation of a resource required
to service this request. Please review the following specific error details
and modify your source code appropriately.

Compiler Error Message: CS1502: The best overloaded method match for
'ASPNetTest.MyItems.Add(ASPNetTest.MyItem)' has some invalid arguments

When I looked at the complete compilation source, It looks like it creates
as Htmlgenericcontrol with a tagname of MyItem. How

Can some one help me how to go about exposing a collection as a public
property in user control that can be set at design time.

Thanks in advance,
j_a5
 
B

bruce barker

you have to create a custom controls to implement the collection and the
items in the collection.

-- bruce (sqlwork.com)
 
J

j_a5

I have a custom object (not a control) MyItem and a collection called
MyItems that is a collection of MyItem. The control is a user control. I
want to build the controls dynamically based on the values. This is how my
aspx page looks:

<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false"
Inherits="ASPNetTest.WebForm4" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm4</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<uc1:webusercontrol1 id="WebUserControl11" runat="server">
<ControlItems>
<uc1:Item Text="Test 1" />
</ControlItems>
</uc1:webusercontrol1></form>
</body>
</HTML>

and the code behind and the custom classes are:

namespace ASPNetTest
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.ComponentModel;
using System.Globalization;

public class WebUserControl1 : System.Web.UI.UserControl
{
private Items controlItems;
private string text;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
int a = 1;

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

public string Text
{
get
{
return text;
}
set
{
text = Text;
}
}

public Items ControlItems
{
get
{
return controlItems;
}
set
{
this.controlItems = value;
}
}
}


public class Item
{
public Item(){}
public Item (string text)
{
this.Text = Text;
}

public string Text;

}

public class Items: System.Collections.CollectionBase
{

public Item this[int index]
{
get
{
return ((Item)List[index]);
}
set
{
List[index] = value;
}
}

public void Remove(int index)
{
if (index > Count - 1 || index < 0)
{
throw new Exception("Index not valid!");
}
else
{
List.RemoveAt(index);
}
}


public int Add(Item value)
{
return (List.Add(value));
}


public int IndexOf (Item value)
{
return (List.IndexOf(value));
}

public void Insert(int index, Item value)
{
List.Insert(index, value);
}

}


}

Can you please clarify.
Thanks,
j_a5
 
G

Guest

I'm going to work through an example and see if I can help, but one question for starters: Do the Collection and the Control work as intended when you set the values through code at run-time?
 
J

j_a5

Yes. It works. and I was able to work around by having a 10 different
properties for each item in the collection (i.e., It can support 10 Items).

However, I figured out why it was not working. to expose items in a
collection, the item must have control as the base type. When I create the
Item as a subclass of Control and register the server control then I can
create the collection as I intended.
i.e.,
1. I made the Item as a subclass of Syste.Web.UI.Control.
2. The Items collection is still as collection of Item
3. Added the Register tab
<% Register TagPrefix="cc1" Namespace ="ASPNetTest" Assembly =
"ASPNetTest." %>

4. Changed the design tag as follows:
<uc1:webusercontrol1 id="WebUserControl11" runat="server">
<ControlItems>
<cc1:Item Text="Test 1" />
</ControlItems>
</uc1:webusercontrol1>

Looks like, for exposing a collection the collection Item has to be some
form of control. I think this is what Bruce Barker meant in his response to
the post.

Thanks,
J_a5

Bill Borg said:
I'm going to work through an example and see if I can help, but one
question for starters: Do the Collection and the Control work as intended
when you set the values through code at run-time?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top