我写的服务器控件(未完,模型如此)
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using System.Resources; using System.Text; using System.Threading.Tasks; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; namespace DecColorPicker { [DefaultProperty("DecColorPickerControl")] [ToolboxData("<{0}:DecColorPickerControl runat=server></{0}:DecColorPickerControl>")] public class DecColorPickerControl : WebControl, IPostBackDataHandler { public string ColorValue { get { string viewstr = this.ID + "_ColorValue"; object text = ViewState[viewstr]; if (text == null) return string.Empty; else return (string)text; } set { string viewstr = this.ID + "_ColorValue"; ViewState[viewstr] = value; } } public string DivContranerID { get { string viewstr = this.ID + "_DivContranerID"; String s = (String)ViewState[viewstr]; return ((s == null) ? this.ID + "DecTextBox" : s); } set { string viewstr = this.ID + "_DivContranerID"; ViewState[viewstr] = value; } } public string PickerBtnID { get { string viewstr = this.ID + "_PickerBtnID"; String s = (String)ViewState[viewstr]; return ((s == null) ? this.ID + "_PickerBtn" : s); } set { string viewstr = this.ID + "_PickerBtnID"; ViewState[viewstr] = value; } } [Description("设置按钮的文本")] public string PickerBtnText { get { string viewstr = this.ID + "_PickerBtnText"; String s = (String)ViewState[viewstr]; return ((s == null) ? "选择颜色" : s); } set { string viewstr = this.ID + "_PickerBtnText"; ViewState[viewstr] = value; } } [Description("是否显示按钮")] public bool ShowPickerBtn { get { string viewstr = this.ID + "_ShowPickerBtn"; var s = ViewState[viewstr]; return s == null ? true : (Convert.ToBoolean(s)); } set { string viewstr = this.ID + "_ShowPickerBtn"; ViewState[viewstr] = value; } } #region 选择器配置 [Bindable(false), DefaultValue(""), Browsable(true), Description("颜色选择器的宽度"), Category("选择器设置")] public string PickerWidth { get { string viewstr = this.ID + "_PickerWidth"; String s = (String)ViewState[viewstr]; return ((s == null) ? "" : s); } set { string viewstr = this.ID + "_PickerWidth"; ViewState[viewstr] = value; } } [Bindable(false), DefaultValue(""), Browsable(true), Description("颜色选择器的高度"), Category("选择器设置")] public string PickerHeight { get { string viewstr = this.ID + "_PickerHeight"; String s = (String)ViewState[viewstr]; return ((s == null) ? "" : s); } set { string viewstr = this.ID + "_PickerHeight"; ViewState[viewstr] = value; } } #endregion public string Text { get { object text = ViewState["Text"]; if (text == null) return string.Empty; else return (string)text; } set { ViewState["Text"] = value; } } public event EventHandler TextChanged; protected void OnTextChanged(EventArgs e) { if (TextChanged != null) TextChanged(this, e); } public bool LoadPostData(string postDataKey, NameValueCollection postCollection) { string postedData = postCollection[postDataKey];//回发回来的字符串 if (!this.ColorValue.Equals(postedData)) { this.ColorValue = postedData; return true; } else { return false; } } public void RaisePostDataChangedEvent() { OnTextChanged(EventArgs.Empty); } protected override void Render(HtmlTextWriter writer) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("<div id="{0}">", this.DivContranerID); sb.AppendFormat("<script>{0}</script>", DecColorPicker.Properties.Resources.colorPicker); //注意此处必须为UniqueID 原因是与LoadPostData的postDataKey 一一对应 lcc sb.AppendFormat("<input type='text' onclick='colorPicker(event)' id='{0}' name='{0}' />", this.UniqueID); if (this.ShowPickerBtn) { sb.AppendFormat("<input type="button" value="{0}" onclick="document.getElementById('{1}').click();">", this.PickerBtnText, this.UniqueID); } sb.Append("</div>"); writer.Write(sb.ToString()); } } }
注意:
sb.AppendFormat("<input type='text' onclick='colorPicker(event)' id='{0}' name='{0}' />", this.UniqueID);
这个textbox的ID必须为UniqueID,如果填写别的ID将无法进行PostData的回发绑定。
此问题我纠结了很久。才发现的。。。。