DataSource是2.0里面很重要的数据访问控件,不过DataSource类控件在设计视图总是Render成灰色的一块饼,除了ID什么信息也没有,看起不是太爽。不过现在发现了 System.Web.UI.Design.WebControls 这个namespace,“包含可用于扩展对 Web 服务器控件的设计时支持的类”。于是拿最有用的ObjectDataSource来试了试,结果可以做成这样:
上面那个就是定制的DataSource,把常用属性显示在设计视图上,用的时候更方便。下面是主要代码:
VeryObjectDataSource
[DisplayName("VeryObjectOataSource"), Description("VeryDxZ's ObjectDataSource")]
[Designer("DxZ.Common.DataBinding.VeryObjectDataSourceDesigner")]
public class VeryObjectDataSource : ObjectDataSource
{
}
public class VeryObjectDataSourceDesigner : System.Web.UI.Design.WebControls.ObjectDataSourceDesigner
{
public override string GetDesignTimeHtml()
{
VeryObjectDataSource veryODS = (VeryObjectDataSource)this.Component;
StringBuilder htmlStrBuilder = new StringBuilder();
htmlStrBuilder.Append("<div style='margin:2px; padding:2px; background-color: buttonface; color: windowtext; font: messagebox;'>");
htmlStrBuilder.AppendFormat("<strong>{0}</strong> - {1}<br/>", veryODS.GetType().Name, veryODS.ID);
if (veryODS.TypeName != null && veryODS.TypeName != string.Empty)
htmlStrBuilder.AppendFormat("TypeName={0}<br/>", veryODS.TypeName);
if (veryODS.DataObjectTypeName != null && veryODS.DataObjectTypeName != string.Empty)
htmlStrBuilder.AppendFormat("DataObject={0}<br/>", veryODS.DataObjectTypeName);
if (veryODS.SelectMethod != null && veryODS.SelectMethod != string.Empty)
{
htmlStrBuilder.Append("HasMethod=Select");
if (veryODS.InsertMethod != null && veryODS.InsertMethod != string.Empty)
htmlStrBuilder.Append(",Insert");
if (veryODS.UpdateMethod != null && veryODS.UpdateMethod != string.Empty)
htmlStrBuilder.Append(",Update");
if (veryODS.DeleteMethod != null && veryODS.DeleteMethod != string.Empty)
htmlStrBuilder.Append(",Delete");
htmlStrBuilder.AppendFormat("<br/>Caching={0}, Paging={1}", veryODS.EnableCaching, veryODS.EnablePaging);
}
htmlStrBuilder.Append("</div>");
return htmlStrBuilder.ToString();
}
}
注意VeryObjectDataSource类的Attribute:
[Designer(
"DxZ.Common.DataBinding.VeryObjectDataSourceDesigner")]。
没有这句来指明Designer的话控件不会依照自定义的去Render。源代码见这里 上面这些只是外观的定制,当然还可以在ObjectDataSource构造函数加一些初始化的参数等等,方便程序设计时使用。同时,这种定制外观的方法对Xml和Substitution等同样Render不出什么内容的控件也适用哦。