我写的服务器控件(未完,模型如此)

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的回发绑定。

此问题我纠结了很久。才发现的。。。。

服务器控件数据回发实现IPostBackDataHandler需注意的的更多相关文章

  1. 在 ASP.NET 网页中不经过回发而以编程方式实现客户端回调

    在 ASP.NET 网页的默认模型中,用户会与页交互,单击按钮或执行导致回发的一些其他操作.此时将重新创建页及其控件,并在服务器上运行页代码,且新版本的页被呈现到浏览器.但是,在有些情况下,需要从客户 ...

  2. 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。

    问题补充: “/Source”应用程序中的服务器错误. 回发或回调参数无效.在配置中使用 <pages enableEventValidation="true"/> 或 ...

  3. asp.net 回发或回调参数无效的各种情况分析及解决办法

    昨天,在实现级联菜单的时候,突然出现一下错误: 回发或回调参数无效.在配置中使用 <pages enableEventValidation="true"/> 或在页面中 ...

  4. 点击datalist中Button按钮出现“回发或回调参数无效......”

        遇到问题: 回发或回调参数无效.在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page ...

  5. FineUIMvc随笔 - 不能忘却的回发(__doPostBack)

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 用户反馈 有网友在官方论坛抛出了这么一个问题,似乎对 FineUIMvc 中的浏览器端与服务器端的交互方式很有异议. 这里面的关 ...

  6. 转:运行page页面时的事件执行顺序及页面的回发与否深度了解

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  7. FineUIMvc随笔(3)不能忘却的回发(__doPostBack)

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 用户反馈 有网友在官方论坛抛出了这么一个问题,似乎对 FineUIMvc 中的浏览器端与服务器端的交互方式很有异议. 这里面的关 ...

  8. System.ArgumentException: 回发或回调参数无效。在配置中使用 < pages enableEventValidation="true"/>

    转载自http://blog.csdn.net/dongge825/article/details/7868151 关于在同一个页面中使用Gridview控件的时候发现气updaeting事件无法被服 ...

  9. 回发或回调参数无效。在配置中使用 pages enableEventValidation=true 或在页面中使用 %@ Page EnableEventValidation=true % 启用了事件验证

    WebForm中回发或回调参数无效问题的解决 解决 .NET中回发或回调参数无效问题的解 该错误的详细提示信息为: 回发或回调参数无效.在配置中使用 <pages enableEventVali ...

随机推荐

  1. FIR数字信号滤波器

    数字滤波器 在信号处理领域中,对于信号处理的实时性.快速性的要求越来越高.而在许多信息处理过程中,如对信号的过滤.检测.预测等,都要广泛地用到滤波器. 其中数字滤波器具有稳定性高.精度高.设计灵活.实 ...

  2. 解决Maven提示:Could not read settings.xml, assuming default values

    本文转载自:http://blog.csdn.net/hqocshheqing/article/details/47702049 最近在学习Maven  时总是出现 Could not read se ...

  3. java数组复制===clone()

    总结:使用方法原理弄清楚 package com.a; public class gjsopb { public static void main(String[] args) { int a[] = ...

  4. selenium自动化浏览器后台运行headless模式

    通过selenium做WEB自动化的时候,必须要启动浏览器, 浏览器的启动与关闭会影响执行效率. 当我们在自己电脑运行代码时,还会影响做别的事情. 鉴于这种情况,Google针对Chrome浏览器新增 ...

  5. 用css实现图片在div内的垂直居中

    已知一个div内有一个img,两者的高度均不知道,但图片的高度一定小于div的高度 代码如下: .box { /*非IE的主流浏览器识别的垂直居中的方法*/ display: table-cell;  ...

  6. CVE-2017-8464(震网三代)复现

    开启msf root@sch01ar:~# msfconsole 设置模块 msf > use exploit/windows/fileformat/cve_2017_8464_lnk_rce ...

  7. js中的Generators函数

    js中的Generators函数 generator一般操作 generator函数的作用就是函数体分段执行,yield表示分隔点 function *test() { console.log(1); ...

  8. Python遍历列表删除多个列表元素

    在遍历list的时候,删除符合条件的数据,结果不符合预期 num_list = [1, 2, 2, 2, 3] print(num_list) for item in num_list: if ite ...

  9. linux信号量使用

    #include <pthread.h> #include <semaphore.h> #include <unistd.h> #include <stdio ...

  10. Android开发笔记 TableLayout常用的属性介绍

    今天看了安卓简单控件的布局方式,大概有绝对.相对.表格.线性.帧式布局五种方式,表格布局里面的一些属性相对来说比较复杂,下面主要谈谈表格方式布局的一些属性   TableLayout经常用到的属性有: ...