2009-06-25 3 views
0

Je crée un contrôle de case à cocher personnalisé pour ajouter un div après chaque case dans une checkboxlist. Le cours est ci-dessous.Contrôle de liste de cases à cocher personnalisé Erreur dans asp.net. "startIndex ne peut pas être plus grand que la longueur de la chaîne"

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 & "_") + 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(">") 

     writer.Write(Items(repeatIndex).Text) 
     ' writer.Write("<div id=" & "mynewDiv" & Items(repeatIndex).Value & "></div>") 


    End Sub 



End Class 

Mais j'obtiens une erreur lorsque je l'utilise dans une page et appelle la sauvegarde. L'erreur est:

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.

Exception Details: System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex

Source Error:

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.

Stack Trace:

[ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex]
System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +7492915
System.Web.UI.WebControls.CheckBoxList.LoadPostData(String postDataKey, NameValueCollection postCollection) +60
System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +13
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +346
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1743

Des idées?

+0

J'ai trouvé la solution j'ai changé le nom pour être "writer.WriteAttribute (" nom "UniqueID et "$" et repeatIndex)" http: // www .jasonlinham.co.uk/2009/04/checkboxlist-startindex-ne-pas-be.html Merci au blog de Jason Linham. – Greens

Répondre

0

Placez un point d'arrêt sur votre déclaration de fonction override renderItem. Démarrez votre projet en utilisant le mode de débogage VS et tracez quelles variables sont définies et à quoi. J'ai le sentiment que vous utilisez un indice de départ de 1 sur une chaîne vide quelque part. Rappelez-vous, les tableaux et tel départ à 0.

Questions connexes