CompositeControl的开发故名思义就是复合控件的开发,关于控件的开发我觉得还是有一定的套路是可以遵循的。
定义控件的服务器端属性
Code
/// <summary>
/// 脚本路径
/// </summary>
[Bindable(true), Browsable(true), DefaultValue("../js/sg/sg_AutoFilter.js")]
public string ScriptPath
{
get
{
if (ViewState["ScriptPath"] != null)
return (string)ViewState["ScriptPath"];
return "../js/sg/sg_AutoFilter.js";
}
set
{
ViewState["ScriptPath"] = value;
}
}
/// <summary>
/// 脚本路径
/// </summary>
[Bindable(true), Browsable(true), DefaultValue("../js/sg/sg_AutoFilter.js")]
public string ScriptPath
{
get
{
if (ViewState["ScriptPath"] != null)
return (string)ViewState["ScriptPath"];
return "../js/sg/sg_AutoFilter.js";
}
set
{
ViewState["ScriptPath"] = value;
}
}
定义该服务器控件的开始标签
Code
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Div;
}
}
给服务器控件开始标签添加样式属性
Code
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Style, string.Format("{0}px",Width));
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Style, string.Format("{0}px",Width));
}
添加标签内的子控件
Code
protected override void CreateChildControls()
{
private HtmlInputText txtTargetControl = new HtmlInputText();
private HiddenField txtTargetControlValue = new HiddenField();
private System.Web.UI.WebControls.Image imgpopup;
Controls.Clear();
//txtTargetControl = new HtmlInputText();
txtTargetControl.ID = "txtTargetControl";
txtTargetControl.Value = this.Text;
txtTargetControl.Attributes.Add("autocomplete", "off");
txtTargetControl.Attributes.Add("style", string.Format("border:0px solid #9abfdc;{0}px", (this.Width - 17).ToString()));
imgpopup = new System.Web.UI.WebControls.Image();
imgpopup.ID = "imgpopup";
imgpopup.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
imgpopup.Attributes.Add("title", "选择公司后请单击此按钮关闭公司选择框!");
if (PopupImageUrl == "")
imgpopup.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Sagacity.Controls.popup.gif");
else
imgpopup.ImageUrl = PopupImageUrl;
//txtTargetControlValue = new HiddenField();
txtTargetControlValue.ID = "txtTargetControlValue";
txtTargetControlValue.Value = this.Value;
Controls.Add(txtTargetControl);
Controls.Add(txtTargetControlValue);
Controls.Add(imgpopup);
ChildControlsCreated = true;
}
protected override void CreateChildControls()
{
private HtmlInputText txtTargetControl = new HtmlInputText();
private HiddenField txtTargetControlValue = new HiddenField();
private System.Web.UI.WebControls.Image imgpopup;
Controls.Clear();
//txtTargetControl = new HtmlInputText();
txtTargetControl.ID = "txtTargetControl";
txtTargetControl.Value = this.Text;
txtTargetControl.Attributes.Add("autocomplete", "off");
txtTargetControl.Attributes.Add("style", string.Format("border:0px solid #9abfdc;{0}px", (this.Width - 17).ToString()));
imgpopup = new System.Web.UI.WebControls.Image();
imgpopup.ID = "imgpopup";
imgpopup.Style.Add(HtmlTextWriterStyle.Cursor, "hand");
imgpopup.Attributes.Add("title", "选择公司后请单击此按钮关闭公司选择框!");
if (PopupImageUrl == "")
imgpopup.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "Sagacity.Controls.popup.gif");
else
imgpopup.ImageUrl = PopupImageUrl;
//txtTargetControlValue = new HiddenField();
txtTargetControlValue.ID = "txtTargetControlValue";
txtTargetControlValue.Value = this.Value;
Controls.Add(txtTargetControl);
Controls.Add(txtTargetControlValue);
Controls.Add(imgpopup);
ChildControlsCreated = true;
}
给标签内的控件添加样式属性
Code
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Style, string.Format("border:1px solid #9abfdc;{0}px", this.Width));
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "0px");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
txtTargetControl.RenderControl(writer);
txtTargetControlValue.RenderControl(writer);
writer.RenderEndTag();
writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingLeft, "0px");
writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "0px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "17px");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
imgpopup.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
writer.AddAttribute(HtmlTextWriterAttribute.Style, string.Format("border:1px solid #9abfdc;{0}px", this.Width));
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "0px");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
txtTargetControl.RenderControl(writer);
txtTargetControlValue.RenderControl(writer);
writer.RenderEndTag();
writer.AddAttribute(HtmlTextWriterAttribute.Align, "left");
writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingLeft, "0px");
writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "0px");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "17px");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
imgpopup.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
添加事件
Code
protected static readonly object _Click = new object();
public event EventHandler Click
{
add { Events.AddHandler(_Click, value);}
remove {Events.RemoveHandler(_Click, value);}
}
protected static readonly object _Click = new object();
public event EventHandler Click
{
add { Events.AddHandler(_Click, value);}
remove {Events.RemoveHandler(_Click, value);}
}
在一定的时候引发该事件,并处理事件,如
Code
protected virtual void OnInit(EventArgs e)
{
EventHandler initHandler = (EventHandler)Events[_Click];
if (initHandler != null)
{
RaiseEvents(this, e);
}
}
private void RaiseEvents(object sender,EventArgs e)
{
//具体代码
}
protected virtual void OnInit(EventArgs e)
{
EventHandler initHandler = (EventHandler)Events[_Click];
if (initHandler != null)
{
RaiseEvents(this, e);
}
}
private void RaiseEvents(object sender,EventArgs e)
{
//具体代码
}
当然如果想做出专业级的控件,还有很多问题需要考虑。如设计时的支持,比较客户端js的编写,用户的体验等等都是我们需要考虑的问题
这里只是提及一般的控件开发过程。