Custom CheckBoxList "Length cannot be less than zero" error

M

MattB

Hello I have a custom CheckBoxList control so I can add attributes to
listitems....
All works fine and I have a RadioButtonList that does the same, but
for this when postingback multiple times and then changing the
selected option I get the error

Length cannot be less than zero. Parameter name: length

further down in the trace output:

aspx.page Begin ProcessPostData 0.006315 0.000024
Unhandled Execution Error
Length cannot be less than zero.
Parameter name: length
at System.String.Substring(Int32 startIndex, Int32 length)
at System.String.Substring(Int32 startIndex)
at System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(String
postDataKey, NameValueCollection postCollection)
at System.Web.UI.Page.ProcessPostData(NameValueCollection postData,
Boolean fBeforeLoad)
at System.Web.UI.Page.ProcessRequestMain()

here's the control, any ideas thanks!
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace myUI
{
/// <summary>
/// Summary description for AttributesCheckBoxList.
/// </summary>
public class AttributesCheckBoxList :
System.Web.UI.WebControls.CheckBoxList
{
protected override void Render(HtmlTextWriter writer)
{
if (HasControls())
{
int i = 0;
foreach (ListItem listItem in Items)
{
writer.WriteBeginTag("input");
writer.WriteAttribute("ID",this.UniqueID+"_"+i );
writer.WriteAttribute("type", "checkbox");
writer.WriteAttribute("name", this.UniqueID);
writer.WriteAttribute("value", listItem.Value, true);
if (listItem.Selected)
writer.WriteAttribute("checked","true");
listItem.Attributes.Render(writer);
writer.Write('>');
writer.WriteEndTag("input");
writer.WriteBeginTag("label");
writer.WriteAttribute("for",this.UniqueID+"_"+i );
listItem.Attributes.Render(writer);
writer.Write('>');
HttpUtility.HtmlEncode(listItem.Text, writer);
writer.WriteEndTag("label");
if (i < Items.Count-1)
writer.RenderBeginTag(HtmlTextWriterTag.Br);
writer.WriteLine();
i++;
}
}
}
}
}
 
J

Juno

Hi,

Could you point out which line cause the error? Why do you believe the error
is in the Render?
I think this error is about a string which is empty or null.
 
B

Bill Cohagan

Juno
I'm getting precisely the same error (including traceback); however in my
case I'm just calling base.render(...). If I subclass the Listbox instead of
the CheckBoxList, then the code works fine. Since the error is being thrown
in the framework code I can't even step through to see what the problem is.

Any ideas what might cause this -- or how I might proceed to troubleshoot?

Thanks in advance,
Bill
 
B

Bill Cohagan

I've figured it out. The subclass was registering a hidden control using the
ID for the control. The base checkboxlist control expects to find a series
of 0 or more hidden controls with ID values foo:0, foo:2, etc. where "foo"
is the control's ID and the attached integer locates the checkbox within the
list. The associated value is "on".

When the Load PostData method apparently can't handle the case where there
is an ID "foo" without the ":" (or any other character following.) Instead
it indexes off the end of the string looking for the index. I think this is
actually a bug in the implementation of LoadPostData for CheckBoxList.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top