title: Custom Checkbox list control Error in asp.net. "startIndex cannot be larger than length of string"
body: <p>I am creating a custom checkbox control to add a div after each checkbox in a checkboxlist.
  The class is below.</p>
  
  <pre><code>Imports System.Web.UI.WebControls
  Imports System.ComponentModel
  Imports System.Globalization
  
  Public Class MyCheckboxListCheckBox
      Inherits CheckBoxList
      Implements IRepeatInfoUser
      Protected Overrides Sub RenderItem(ByVal itemType As ListItemType, ByVal repeatIndex As Integer, ByVal repeatInfo As RepeatInfo, ByVal writer As HtmlTextWriter)
  
          writer.WriteBeginTag("input")
          writer.WriteAttribute("type", "checkbox")
          writer.WriteAttribute("name", UniqueID)
          writer.WriteAttribute("id", (ClientID &amp; "_") + repeatIndex.ToString(NumberFormatInfo.InvariantInfo))
          writer.WriteAttribute("value", Items(repeatIndex).Value)
          Dim attrs As System.Web.UI.AttributeCollection = Items(repeatIndex).Attributes
          For Each key As String In attrs.Keys
              writer.WriteAttribute(key, attrs(key))
          Next
          writer.Write("&gt;")
  
          writer.Write(Items(repeatIndex).Text)
          ' writer.Write("&lt;div id=" &amp; "mynewDiv" &amp; Items(repeatIndex).Value &amp; "&gt;&lt;/div&gt;")
  
  
      End Sub
  
  
  
  End Class
  </code></pre>
  
  <p>But I get an error when i use it in a page and call the save. The error is :</p>
  
  <blockquote>
    <p>startIndex cannot be larger than
    length of string. Parameter name:
    startIndex  Description: An unhandled
    exception occurred during the
    execution of the current web request.
    Please review the stack trace for more
    information about the error and where
    it originated in the code.</p>
    
    <p>Exception Details:
    System.ArgumentOutOfRangeException:
    startIndex cannot be larger than
    length of string. Parameter name:
    startIndex</p>
    
    <p>Source Error: </p>
    
    <p>An unhandled exception was generated
    during the execution of the current
    web request. Information regarding the
    origin and location of the exception
    can be identified using the exception
    stack trace below.  </p>
    
    <p>Stack Trace: </p>
    
    <p>[ArgumentOutOfRangeException:
    startIndex cannot be larger than
    length of string. Parameter name:
    startIndex]<br>
    System.String.InternalSubStringWithChecks(Int32
    startIndex, Int32 length, Boolean
    fAlwaysCopy) +7492915<br>
    System.Web.UI.WebControls.CheckBoxList.LoadPostData(String
    postDataKey, NameValueCollection
    postCollection) +60<br>
    System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(String
    postDataKey, NameValueCollection
    postCollection) +13<br>
    System.Web.UI.Page.ProcessPostData(NameValueCollection
    postData, Boolean fBeforeLoad) +346<br>
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean
    includeStagesAfterAsyncPoint) +1743</p>
  </blockquote>
  
  <p>Any ideas ? </p>