我正在创建一个自定义的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的CheckBox列表和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或名称。有什么建议吗?
答案 0 :(得分:0)
我想到的唯一可能的原因是这一行:
cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack(\\'" + cb.ClientName + "\\',\\'\\')', 0)");
您不需要那些\\
cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack('" + cb.ClientName + "','')', 0)");