Hi Andrew,
I have wrote a demo that may help to solve most of your concern. Please try
the following code first:
Aspx:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:MyReorderList runat="server" ID="MyReorderList1"
SortOrderField="Priority">
<HeaderTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><asp:Button
ID="Button2"
runat="server" Text="Test Header" />
</HeaderTemplate>
<FooterTemplate>
<asp:Label ID="Label2" runat="server" Text="I'm a footer

"
BackColor="Pink"></asp:Label>
</FooterTemplate>
</cc1:MyReorderList>
Aspx.cs:
public class MyObject
{
public int Priority { get; set; }
public bool Flag { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<MyObject> list = new List<MyObject>();
for (int i = 0; i < 10; i++) {
list.Add(new MyObject() { Priority = i });
}
list[1].Address = "Address1";
list[1].Flag = true;
list[3].Address = "Address3";
list[3].Name = "Allen";
list[5].Flag = true;
this.MyReorderList1.ItemTemplate = new
MyTemplate(typeof(MyObject), "Priority");
this.MyReorderList1.DataSource = list;
this.MyReorderList1.DataBind();
}
}
public class MyTemplate : ITemplate
{
Type type;
string FieldColName;
public MyTemplate( Type t, string fieldname)
{
type = t;
this.FieldColName = fieldname;
}
public void InstantiateIn(Control objContainer)
{
Panel p = new Panel();
Label lbl = new Label();
foreach (var item in MyReorderList.MyMapping)
{
Control c = Activator.CreateInstance(item.Value) as Control;
if (c != null) {
c.Visible = false;
p.Controls.Add(c);
}
}
p.Controls.Add(lbl);
lbl.DataBinding += new EventHandler(lblHeader_DataBinding);
objContainer.Controls.Add(p);
}
private void lblHeader_DataBinding(object sender, EventArgs e)
{
object bound_value_obj = null;
Label lbl = (Label)sender;
IDataItemContainer data_item_container =
(IDataItemContainer)lbl.NamingContainer;
foreach (var p in
data_item_container.DataItem.GetType().GetProperti es())
{
Type propertytype = p.PropertyType;
bound_value_obj = p.GetValue(data_item_container.DataItem,
null);
Type controltype = null;
bool canfind =
MyReorderList.MyMapping.TryGetValue(propertytype, out controltype);
if (canfind)
{
lbl.Visible = false;
foreach (Control c in lbl.Parent.Controls)
{
if (c.GetType() == controltype)
{
c.Visible = true;
//add logic here if you want to set initial
value
//here is a sample
if (controltype == typeof(CheckBox))
{
if (bound_value_obj is bool)
{
((CheckBox)c).Checked =
(bool)bound_value_obj;
}
}
if (controltype == typeof(Label))
{
if (bound_value_obj != null)
((Label)c).Text =
bound_value_obj.ToString();
else
{
((Label)c).Text = "NULL";
}
}
//if you want more you can add extra logic
}
}
}
else
{
lbl.Text = bound_value_obj.ToString();
}
}
}
}
public class MyReorderList : ReorderList
{
public static Dictionary<Type, Type> MyMapping { get; private set; }
static MyReorderList()
{
//add default mappings.
MyMapping = new Dictionary<Type, Type>();
MyMapping.Add(typeof(string), typeof(Label));
MyMapping.Add(typeof(bool), typeof(CheckBox));
MyMapping.Add(typeof(int), typeof(Button));
}
[DefaultValue((string)null),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(RepeaterItem)), Browsable(false)]
public virtual ITemplate HeaderTemplate
{
get;
set;
}
[DefaultValue((string)null),
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(RepeaterItem)), Browsable(false)]
public virtual ITemplate FooterTemplate
{
get;
set;
}
protected override void Render(HtmlTextWriter writer)
{
Panel header=new Panel();
this.HeaderTemplate.InstantiateIn(header);
header.RenderControl(writer);
base.Render(writer);
Panel footer = new Panel();
this.FooterTemplate.InstantiateIn(footer);
footer.RenderControl(writer);
}
}
About the code:
1. The footer and header is simply created via HeaderTemplate and
FooterTemplate. In Render event we can create instance of the footer/header
and render the HTML tags via RenderControl method.
2. To give user flexibility to change the mapping of the control and the
type of the data I added a global mapping MyMapping in MyReorderList class.
There're some predefined mappings added initially and users can add/remove
them when using MyReorderList control.
3. We can add much more functionalities that can provide more flexibility.
But to make it simple I opt to just provide the code above. You can see
though I tried to make it as simple as possible it still involved a lot of
code.
Please try it to see if it's the effect you're looking for and feel free to
let me know if you need further assistance.
Regards,
Allen Chen
Microsoft Online Support