private List<MyObj> _urls = new List<MyObj>();
public List<MyObj> Urls {
get {
return _urls;
}
set {
_urls = value;
}
}
How do I modify this so that I can populate it in my markup, like this <uc1:UrlControl id="test" runat="server"> <MyObj Text="TETST" Value= "SDFSDF" />
Yeah, it is a usercontrol, I want to be able to populate a public property from the aspx instead of the .cs...is there some sort of definition I need to put above the Property?
So for example, the Telerik RadPanel can populate it's items through the markup
As far as I know, it seems that it is impossible to finish it because the user control derives from TemplateControl. Based on my experience, I suggest you try to resolve it by creating a custom control. Please refer to the following code:
Create a class library and named it(such as 'ControlBuilderControl'.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing.Design;
using System.Collections;
using System.Globalization;
using System.ComponentModel.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
namespace MyControls
{
[ToolboxData("<{0}:ControlBuilderControl runat=server></{0}:ControlBuilderControl>")]
[ParseChildren(true, "ScriptItems")]
[ControlBuilder(typeof(ScriptItemBuilder))]
public class ControlBuilderControl : WebControl
{
private ScriptItemCollection _ScriptItems = new ScriptItemCollection();
/// <summary>
/// </summary>
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Description("ToolCategory")]
[Category("Customize tool category")]
public ScriptItemCollection ScriptItems
{
get
{
if (_ScriptItems == null)
{
_ScriptItems = new ScriptItemCollection();
}
return _ScriptItems;
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
// base.RenderContents(writer);
foreach (ScriptItem obj in ScriptItems)
{
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddAttribute(HtmlTextWriterAttribute.Value, obj.Text);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}
}
[ParseChildren(true,"Text")]
public class ScriptItem
{
private string _Text;
[DefaultValue("")]
[Editor(typeof(MultilineStringEditor),typeof(UITypeEditor))]
//[Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design",typeof(UITypeEditor))]
[PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
[NotifyParentProperty(true)]
/// <summary>
/// JavaScript
/// </summary>
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
}
}
}
public class ScriptItemCollection : List<ScriptItem>
{
public ScriptItemCollection() : base() { }
public ScriptItemCollection(int capacity) : base(capacity) { }
public ScriptItemCollection(IEnumerable<ScriptItem> collection)
: base
(collection) { }
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false), Bindable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new int Capacity { get; set; }
}
public class ScriptItemBuilder : ControlBuilder
{
public override Type GetChildControlType(string tagName, IDictionary
attributes)
{
if (string.Compare(tagName.ToLower(), "ScriptItem", false,
CultureInfo.InvariantCulture) == 0)
{
return typeof(ScriptItem);
} return null;
}
public override bool AllowWhitespaceLiterals()
{
return true;
}
}
}
Gary yang - MSFT
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
stevescottho...
Member
117 Points
300 Posts
Usercontrol populate collection in markup
Nov 13, 2009 04:33 PM|LINK
I have a property in my codebehind
private List<MyObj> _urls = new List<MyObj>(); public List<MyObj> Urls { get { return _urls; } set { _urls = value; } }
How do I modify this so that I can populate it in my markup, like this
<uc1:UrlControl id="test" runat="server">
<MyObj Text="TETST" Value= "SDFSDF" />
Does that make sense?
thuhue
All-Star
15625 Points
3146 Posts
Re: Usercontrol populate collection in markup
Nov 13, 2009 05:16 PM|LINK
I don't quite understand what your problem is.
Please provide markup and codebehind code.
sayyazahmad
Member
469 Points
92 Posts
Re: Usercontrol populate collection in markup
Nov 13, 2009 05:33 PM|LINK
It won't work. Yount cannot access the property from aspx like this. Rather you should crete a user control.
stevescottho...
Member
117 Points
300 Posts
Re: Usercontrol populate collection in markup
Nov 13, 2009 06:11 PM|LINK
Yeah, it is a usercontrol, I want to be able to populate a public property from the aspx instead of the .cs...is there some sort of definition I need to put above the Property?
So for example, the Telerik RadPanel can populate it's items through the markup
<telerik:RadPanelItem Text="Mail" ImageUrl="Img/mail.gif" Expanded="True"> <Items> <telerik:RadPanelItem ImageUrl="Img/mailPersonalFolders.gif" Text="Personal Folders" /> <telerik:RadPanelItem ImageUrl="Img/mailDeletedItems.gif" Text="Deleted Items" /> <telerik:RadPanelItem ImageUrl="Img/mailInbox.gif" Text="Inbox" /> <telerik:RadPanelItem ImageUrl="Img/mailFolder.gif" Text="My Mail" /> <telerik:RadPanelItem ImageUrl="Img/mailSent.gif" Text="Sent Items" /> <telerik:RadPanelItem ImageUrl="Img/mailOutbox.gif" Text="Outbox" /> <telerik:RadPanelItem ImageUrl="Img/mailSearch.gif" Text="Search Folders" /> </Items> </telerik:RadPanelItem>I essentially want to do the same thing, except use my own custom object to populate my List<>
Gary yang - ...
All-Star
25901 Points
2588 Posts
Re: Usercontrol populate collection in markup
Nov 18, 2009 06:06 AM|LINK
As far as I know, it seems that it is impossible to finish it because the user control derives from TemplateControl. Based on my experience, I suggest you try to resolve it by creating a custom control. Please refer to the following code:
Create a class library and named it(such as 'ControlBuilderControl'.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI; using System.Web.UI.Design; using System.Web.UI.WebControls; using System.ComponentModel; using System.Drawing.Design; using System.Collections; using System.Globalization; using System.ComponentModel.Design; using System.Windows.Forms.Design; using System.Windows.Forms; namespace MyControls { [ToolboxData("<{0}:ControlBuilderControl runat=server></{0}:ControlBuilderControl>")] [ParseChildren(true, "ScriptItems")] [ControlBuilder(typeof(ScriptItemBuilder))] public class ControlBuilderControl : WebControl { private ScriptItemCollection _ScriptItems = new ScriptItemCollection(); /// <summary> /// </summary> [PersistenceMode(PersistenceMode.InnerDefaultProperty)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] [Description("ToolCategory")] [Category("Customize tool category")] public ScriptItemCollection ScriptItems { get { if (_ScriptItems == null) { _ScriptItems = new ScriptItemCollection(); } return _ScriptItems; } } protected override void RenderContents(HtmlTextWriter writer) { // base.RenderContents(writer); foreach (ScriptItem obj in ScriptItems) { writer.RenderBeginTag(HtmlTextWriterTag.Tr); writer.RenderBeginTag(HtmlTextWriterTag.Td); writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); writer.AddAttribute(HtmlTextWriterAttribute.Value, obj.Text); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); writer.RenderEndTag(); writer.RenderEndTag(); } } } [ParseChildren(true,"Text")] public class ScriptItem { private string _Text; [DefaultValue("")] [Editor(typeof(MultilineStringEditor),typeof(UITypeEditor))] //[Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design",typeof(UITypeEditor))] [PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)] [NotifyParentProperty(true)] /// <summary> /// JavaScript /// </summary> public string Text { get { return _Text; } set { _Text = value; } } } public class ScriptItemCollection : List<ScriptItem> { public ScriptItemCollection() : base() { } public ScriptItemCollection(int capacity) : base(capacity) { } public ScriptItemCollection(IEnumerable<ScriptItem> collection) : base (collection) { } [EditorBrowsable(EditorBrowsableState.Never), Browsable(false), Bindable(false)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public new int Capacity { get; set; } } public class ScriptItemBuilder : ControlBuilder { public override Type GetChildControlType(string tagName, IDictionary attributes) { if (string.Compare(tagName.ToLower(), "ScriptItem", false, CultureInfo.InvariantCulture) == 0) { return typeof(ScriptItem); } return null; } public override bool AllowWhitespaceLiterals() { return true; } } }Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.