0

I am creating a custom CheckBox WebControl which inherits From System.Web.UI.WebControls.CheckBox in ASP.NET. But getting this error when postback:

Input string was not in a correct format.

[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

I have a List of CheckBox which is called ItemList and OnInit I execute this function:

 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);
        }
    }

And I call this method 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();
    }

I guess I am getting this error because generated checkboxes hava a wrong ID or name. Are there any suggestions?

izel
  • 15
  • 1
  • 4

1 Answers1

0

The only possibly reason that comes to my mind is this line:

cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack(\\'" + cb.ClientName + "\\',\\'\\')', 0)");

you dont need those \\

cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack('" + cb.ClientName + "','')', 0)");
Enrique Zavaleta
  • 2,098
  • 3
  • 21
  • 29