Currently I'm working on a extensive form, which I'd like to organise by grouping input fields in groupbox-like panels. These panels have an header, a minimize and pin icon. Also inner controls should be able to perform two-way binding to the FormViews datasource.
I can't find any builtin asp.net 开发者_如何转开发control that does the job. What is the best way to accomplish this feature request? Any examples available on this (can't find a decent one)?
Many thanks!
Templated controls will get the job done. Check out these links for more information:
- http://msdn.microsoft.com/en-us/library/aa478964.aspx
- http://weblogs.asp.net/palermo4/archive/2007/01/10/creating-a-custom-databound-templated-control-in-asp-net-2-0.aspx
Tuturials are interesting, but didn't realy need templated controls at the moment, neither databound controls. Only controls in the controlcollection should be able to do so, but apparently they're able by default.
Found out multiple ways to accomplish. One to extend the default Panel control, the other one to extend from WebControl as shown below. Works as intended.
[DefaultProperty("HeaderText")]
[ToolboxData(@"<{0}:FormGroupBox runat=""server"" HeaderText=""Title""></{0}:FormGroupBox>")]
[PersistChildren(true), ParseChildren(false, "Controls")]
public class FormGroupBox : WebControl
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string HeaderText
{
get
{
String s = (String) ViewState["HeaderText"];
return ((s == null) ? "[" + this.ID + "]" : s);
}
set
{
ViewState["HeaderText"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(string.Format("<strong>{0}</strong>", this.HeaderText));
output.Write(@"<hr />");
base.RenderContents(output);
}
}
精彩评论