WebControl with XML Parameters
For 'spaz99'
Sometimes as you may already know, custom controls can become somewhat large. With many features and abilities that need to be configured as part of their usual operation, usually this is done via the use of custom attributes. However this does get unwieldy on the larger objects.
M$ not being altogether immune from practicality catered for this with ASP.NET by providing the [PersistenceMode(PersistenceMode.InnerProperty)] attribute. This allows us to move sizeable attributes to inner XML based elements instead, while being able to retain the properties in our objects.
An example of which is given below.
namespace InnerPropControlExample
{
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
[ToolboxData(@"<{0}:InnerPropControl runat=""server"">
<Parameters>
<Param Name=""param1"" Value=""value1"" />
<Param Name=""param2"" Value=""value2"" />
<Param Name=""param3"" Value=""value3"" />
</Parameters>
</{0}:InnerPropControl>")]
public class InnerPropControl : WebControl
{
/// <summary>
/// Holds the parameters during object lifetime
/// </summary>
private ParamCollection _par;
/// <summary>
/// Gets or sets the parameters.
/// </summary>
/// <value>The parameters.</value>
/// MRW: 21/02/2007
[Browsable(false)]
public ParamCollection Parameters
{
set { this._par = value; }
get { return this._par; }
}
/// <summary>
/// Renders the control to the specified HTML writer.
/// -- In this case it simply renders out the name/value parameers
/// </summary>
/// <param name="writer">The HtmlTextWriter that receives the content.</param>
protected override void Render(HtmlTextWriter writer)
{
foreach( Param par in this._par )
writer.Write("{0} = {1}<br/>\n", par.Name, par.Value);
base.Render(writer);
}
}
/// <summary>
/// A basic Name/Value container
/// </summary>
[PersistenceMode(PersistenceMode.InnerProperty)]
public class Param
{
public string Name;
public string Value;
}
/// <summary>
/// A collection for the Name/Value container
/// </summary>
[PersistenceMode(PersistenceMode.InnerProperty)]
public class ParamCollection : IList<Param>
{
private List<Param> _col = new List<Param>();
/// <summary>
/// Sets the parameters as they are added by ASP.NET
/// </summary>
/// <value>The param.</value>
public Param Param
{
set { this._col.Add(value); } // notice the Add!
}
// Everything below is pretty much standard...
Param IList<Param>.this[int index]
{
get { return this._col[index]; }
set { this._col[index] = value; }
}
int IList<Param>.IndexOf(Param item)
{
return this._col.IndexOf(item);
}
void IList<Param>.Insert(int index, Param item)
{
this._col.Insert(index, item);
}
void IList<Param>.RemoveAt(int index)
{
this._col.RemoveAt(index);
}
void ICollection<Param>.Add(Param item)
{
this._col.Add(item);
}
void ICollection<Param>.Clear()
{
this._col.Clear();
}
bool ICollection<Param>.Contains(Param item)
{
return this._col.Contains(item);
}
void ICollection<Param>.CopyTo(Param[] array, int arrayIndex)
{
this._col.CopyTo(array, arrayIndex);
}
int ICollection<Param>.Count
{
get { return this._col.Count; }
}
bool ICollection<Param>.IsReadOnly
{
get { return false; }
}
bool ICollection<Param>.Remove(Param item)
{
return this._col.Remove(item);
}
public IEnumerator<Param> GetEnumerator()
{
return this._col.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this._col.GetEnumerator();
}
}
}