Friday, 17 April 2009

CheckBoxList - startIndex cannot be larger than length of string

Have you ever created a custom checkboxlist and then recieved the following error when you submit the form if any of the check boxes are selected?

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



I have and it took quite some Googling to find a solution. The error is caused by the checkbox name not following the Microsoft.NET convensions, i.e. ctl13$ctl02$
The solution was to update the name to use UniqueID + $ + index on the end.

protected override void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
var labelID = UniqueID + "$" + repeatIndex;
writer.Write(string.Format("<input value=\"{1}\" id=\"{0}\" name=\"{0}\" type=\"checkbox\" {2} />",
labelID,
Items[repeatIndex].Value,
Items[repeatIndex].Selected ? " checked=\"checked\"" : ""));
}

12 comments:

Anonymous said...

Thanks for this solution:)

Anonymous said...

Thanks for the soluton.

Kalle said...

Good one! Worked for me like a charm. Apparently this bug is also in the CSSFriendly package. Atleast when you create the controls dynamically.

Rich said...

Thanks for posting this. I almost torn my hair out over the same problem!

Jason Linham said...

I'm glad people are finding this useful. This is the aim of the blog, if I've struggled with a problem and found a solution I hope to share that knowledge so others don't have to struggle.

Anonymous said...

absolute leg-end, this error was popping as a javascript error and I just couldn't work out what the issue was....4 hours of my life I aint gonna get back!

Anonymous said...

thanks...

Anonymous said...

Thanks Thanks your post really help me a lot.. You are Master .. :-)

Anonymous said...

I spend three days for this problem... Thanks a lot!!! Yor post help for me!!! You are wery cool specialist!!!

Anonymous said...

Thank you for the solution, saved lot of time.

Anonymous said...

When using this naming convention the selectedindexchanged event is not firing. Any idea how to make this work

Anonymous said...

Thank you so much!
Worked great :)