------------------------------|
| | |
| |----------------------|
| | |
------------------------------|
模版样式如上,是要从 工具箱中托动控件(自定义的 模版 控件)界面就如上显示(类似表格,好像就是表格)
表格中的单元 格中可以拖动工具箱中其他控件放入其中。如何实现?
| | |
| |----------------------|
| | |
------------------------------|
模版样式如上,是要从 工具箱中托动控件(自定义的 模版 控件)界面就如上显示(类似表格,好像就是表格)
表格中的单元 格中可以拖动工具箱中其他控件放入其中。如何实现?
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls ;namespace CustomControls
{
[
ParseChildren(true)
]
public class TemplatedFirstControl : WebControl , INamingContainer
{
private ITemplate firstTemplate;
private String text = null;
private Control myTemplateContainer;
protected override void OnDataBinding(EventArgs e)
{
EnsureChildControls();
base.OnDataBinding(e);
}
[TemplateContainer(typeof(FirstTemplateContainer))]
public ITemplate ItemTemplate
{
get
{
return firstTemplate;
}
set
{
firstTemplate = value;
}
}
public String Text
{
get
{
return text;
}
set
{
text = value;
}
}
public String DateTime
{
get
{
return System.DateTime.Now.ToLongTimeString();
}
}
public Control MyTemplateContainer
{
get
{
return myTemplateContainer;
}
}
protected override void CreateChildControls ()
{
if (ItemTemplate != null)
{
myTemplateContainer = new FirstTemplateContainer(this);
ItemTemplate.InstantiateIn(myTemplateContainer);
Controls.Add(myTemplateContainer);
}
else
{
Controls.Add(new LiteralControl(Text + " " + DateTime));
}
}
} //下面的控件是该模板的容器。
public class FirstTemplateContainer : Control, INamingContainer
{
private TemplatedFirstControl parent;
public FirstTemplateContainer(TemplatedFirstControl parent)
{
this.parent = parent;
}
public String Text
{
get
{
return parent.Text;
}
}
public String DateTime
{
get
{
return parent.DateTime;
}
}
}}
// TemplatedListDesigner.cs.
//------------------------------------------------------------------------namespace CustomControls
{ using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Diagnostics;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.WebControls; public class TemplatedListDesigner : TemplatedControlDesigner
{
private TemplateEditingVerb[] templateVerbs;
private bool templateVerbsDirty; public TemplatedListDesigner()
{
templateVerbsDirty = true;
} public override bool AllowResize
{
get
{
// When templates are not defined, render a read-only fixed-
// size block. Once templates are defined or are being edited, the control should allow
// resizing.
return TemplatesExist || InTemplateMode;
}
} public override bool DesignTimeHtmlRequiresLoadComplete
{
get
{
// If there is a data source, look it up in the container
// and require the document to be loaded completely.
return true;
}
} protected bool TemplatesExist
{
get
{
return (((TemplatedFirstControl)Component).ItemTemplate != null);
}
} protected override ITemplateEditingFrame CreateTemplateEditingFrame(TemplateEditingVerb verb)
{
ITemplateEditingService teService = (ITemplateEditingService)GetService(typeof(ITemplateEditingService));
Debug.Assert(teService != null, "How did we get this far without an ITemplateEditingService");
Debug.Assert(verb.Index == 0); string[] templateNames = new string[] { "ItemTemplate" };
Style[] templateStyles = new Style[] { ((TemplatedFirstControl)Component).ControlStyle }; ITemplateEditingFrame editingFrame =
teService.CreateFrame(this, verb.Text, templateNames, ((TemplatedFirstControl)Component).ControlStyle, templateStyles);
return editingFrame;
} protected override void Dispose(bool disposing)
{
if (disposing)
{
DisposeTemplateVerbs();
}
base.Dispose(disposing);
} private void DisposeTemplateVerbs()
{
if (templateVerbs != null)
{
templateVerbs[0].Dispose(); templateVerbs = null;
templateVerbsDirty = true;
}
} protected override TemplateEditingVerb[] GetCachedTemplateEditingVerbs()
{
if (templateVerbsDirty == true)
{
DisposeTemplateVerbs(); templateVerbs = new TemplateEditingVerb[1];
templateVerbs[0] = new TemplateEditingVerb("Item Template", 0, this); templateVerbsDirty = false;
} return templateVerbs;
}
public override DesignerVerbCollection Verbs
{
get
{
return new DesignerVerbCollection(
new DesignerVerb[] {
new DesignerVerb("Test Command",new EventHandler(Button1_Click)) }); // return GetCachedTemplateEditingVerbs();
}
} private void Button1_Click(object sender, System.EventArgs e)
{
CreateTemplateEditingFrame(templateVerbs[0]);
}
public override string GetDesignTimeHtml()
{
TemplatedFirstControl control = (TemplatedFirstControl)Component;
string designTimeHTML = null;
bool hasATemplate = this.TemplatesExist; if (hasATemplate)
{
bool dummyDataSource;
//IEnumerable designTimeDataSource = GetDesignTimeDataSource(5, out dummyDataSource); try
{
// control.DataSource = designTimeDataSource;
// control.DataBind(); designTimeHTML = base.GetDesignTimeHtml();
}
finally
{
//control.DataSource = null;
}
}
else
{
designTimeHTML = GetEmptyDesignTimeHtml();
} return designTimeHTML;
} protected override string GetEmptyDesignTimeHtml()
{
string text; if (CanEnterTemplateMode)
{
text = "Right-click and choose a set of templates to edit their content.<br>The ItemTemplate is required.";
}
else
{
text = "Switch to HTML view to edit the control's templates.<br>The ItemTemplate is required.";
}
return CreatePlaceHolderDesignTimeHtml(text);
} protected override string GetErrorDesignTimeHtml(Exception e)
{
Debug.Fail(e.ToString());
return CreatePlaceHolderDesignTimeHtml("There was an error rendering the control.<br>Check to make sure all properties are valid.");
} public override IEnumerable GetTemplateContainerDataSource(string templateName)
{
return ((IDataSourceProvider)this).GetResolvedSelectedDataSource();
} public override string GetTemplateContainerDataItemProperty(string templateName)
{
return "DataItem";
} public override string GetTemplateContent(ITemplateEditingFrame editingFrame, string templateName, out bool allowEditing)
{
Debug.Assert(editingFrame.Verb.Index == 0);
Debug.Assert(templateName.Equals("ItemTemplate"));
allowEditing = true; ITemplate template = ((TemplatedFirstControl)Component).ItemTemplate;
string templateContent = String.Empty; if (template != null)
{
templateContent = GetTextFromTemplate(template);
} return templateContent;
} public override void OnComponentChanged(object sender, ComponentChangedEventArgs e)
{
if (e.Member != null)
{
string memberName = e.Member.Name;
if (memberName.Equals("DataSource") || memberName.Equals("DataMember"))
{
//OnDataSourceChanged();
}
else if (memberName.Equals("ItemStyle"))
{
OnStylesChanged();
}
} base.OnComponentChanged(sender, e);
} protected void OnStylesChanged()
{
OnTemplateEditingVerbsChanged();
} protected void OnTemplateEditingVerbsChanged()
{
templateVerbsDirty = true;
} protected override void PreFilterProperties(IDictionary properties)
{
base.PreFilterProperties(properties); PropertyDescriptor prop; prop = (PropertyDescriptor)properties["DataSource"];
Debug.Assert(prop != null);
prop = TypeDescriptor.CreateProperty(this.GetType(), prop,
new Attribute[] {
new TypeConverterAttribute(typeof(DataSourceConverter))
});
properties["DataSource"] = prop;
} public override void SetTemplateContent(ITemplateEditingFrame editingFrame, string templateName, string templateContent)
{
Debug.Assert(editingFrame.Verb.Index == 0);
Debug.Assert(templateName.Equals("ItemTemplate")); ITemplate template = null; if ((templateContent != null) && (templateContent.Length != 0))
{
template = GetTemplateFromText(templateContent);
} ((TemplatedFirstControl)Component).ItemTemplate = template;
}
}
}