[英]Custom CheckBoxList WebControl Error
我正在创建一个自定义CheckBox WebControl,它继承自ASP.NET中的System.Web.UI.WebControls.CheckBox。 但是在回发时出现此错误:
输入的字符串格式不正确。
[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10896279
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145
System.Web.UI.WebControls.CheckBoxList.LoadPostData(String postDataKey, NameValueCollection postCollection) +140
System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +16
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +734
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1960
我有一个名为ItemList和OnInit的CheckBox列表,我执行此功能:
internal void InitItems()
{
for (int i = 0; i < this.Items.Count; i++)
{
CheckBox cb = new CheckBox()
{
LabelPosition = CheckBox.LabelPositions.None
};
ItemList.Add(cb);
Controls.Add(cb);
}
}
我将此方法称为OnRender:
public string RenderItems() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.Items.Count; i++)
{
ListItem item = this.Items[i];
CheckBox cb = ItemList[i];
cb.Text = item.Text;
cb.Enabled = Enabled ? item.Enabled : Enabled;
cb.Checked = item.Selected;
cb.ID = i.ToString();
cb.ViewType = (CheckBox.ViewTypes) Enum.Parse(typeof(CheckBox.ViewTypes),this.ViewType.ToString());
cb.DisplayType = (CheckBox.DisplayTypes)Enum.Parse(typeof(CheckBox.DisplayTypes), this.DisplayType.ToString());
if (this.AutoPostBack)
cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack(\\'" + cb.ClientName + "\\',\\'\\')', 0)");
sb.Append(cb.GetHtml());
}
return sb.ToString();
}
我猜我正在收到此错误,因为生成的复选框具有错误的ID或名称 。 有什么建议吗?
我想到的唯一可能原因是此行:
cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack(\\'" + cb.ClientName + "\\',\\'\\')', 0)");
您不需要那些\\\\
cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack('" + cb.ClientName + "','')', 0)");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.