我正在创建一个自定义 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
我有一个 CheckBox 列表,称为 ItemList 和 OnInit 我执行此函数:
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 或名称。有什么建议吗?