Custom CheckBoxList "Length cannot be less than zero" error

Discussion in 'ASP .Net Building Controls' started by MattB, Aug 2, 2004.

  1. MattB

    MattB Guest

    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++;
    }
    }
    }
    }
    }
     
    MattB, Aug 2, 2004
    #1
    1. Advertising

  2. MattB

    Juno Guest

    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.

    --
    Juno
    MCSD.NET, MCDBA, MCSE
    ----------------------------------------------------------
    Support Team of EasyDotNet, INC. http://www.EasyDotNet.com
    DataForm.NET - The most powerful data entry web server control for ASP.NET



    "MattB" <> wrote in message
    news:...
    > 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.Lo
    adPostData(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++;
    > }
    > }
    > }
    > }
    > }
     
    Juno, Aug 2, 2004
    #2
    1. Advertising

  3. MattB

    MattB Guest

    thanks got it
    writer.WriteAttribute("name", this.UniqueID+":"+i);


    "Juno" <> wrote in message news:<>...
    > 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.
    >
    > --
    > Juno
    > MCSD.NET, MCDBA, MCSE
    > ----------------------------------------------------------
    > Support Team of EasyDotNet, INC. http://www.EasyDotNet.com
    > DataForm.NET - The most powerful data entry web server control for ASP.NET
    >
    >
    >
    > "MattB" <> wrote in message
    > news:...
    > > 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.Lo
    > adPostData(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++;
    > > }
    > > }
    > > }
    > > }
    > > }
     
    MattB, Aug 3, 2004
    #3
  4. MattB

    Bill Cohagan Guest

    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

    "Juno" <> wrote in message
    news:...
    > 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.
    >
    > --
    > Juno
    > MCSD.NET, MCDBA, MCSE
    > ----------------------------------------------------------
    > Support Team of EasyDotNet, INC. http://www.EasyDotNet.com
    > DataForm.NET - The most powerful data entry web server control for ASP.NET
    >
    >
    >
    > "MattB" <> wrote in message
    > news:...
    > > 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.Lo
    > adPostData(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++;
    > > }
    > > }
    > > }
    > > }
    > > }

    >
    >
     
    Bill Cohagan, Sep 28, 2004
    #4
  5. MattB

    Bill Cohagan Guest

    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

    "Bill Cohagan" <> wrote in message
    news:...
    > 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
    >
    > "Juno" <> wrote in message
    > news:...
    > > 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.
    > >
    > > --
    > > Juno
    > > MCSD.NET, MCDBA, MCSE
    > > ----------------------------------------------------------
    > > Support Team of EasyDotNet, INC. http://www.EasyDotNet.com
    > > DataForm.NET - The most powerful data entry web server control for

    ASP.NET
    > >
    > >
    > >
    > > "MattB" <> wrote in message
    > > news:...
    > > > 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.Lo
    > > adPostData(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++;
    > > > }
    > > > }
    > > > }
    > > > }
    > > > }

    > >
    > >

    >
    >
     
    Bill Cohagan, Sep 28, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SG96aQ==?=
    Replies:
    1
    Views:
    6,961
    Ken Cox [Microsoft MVP]
    Jun 2, 2004
  2. Replies:
    2
    Views:
    5,934
  3. ctk70
    Replies:
    2
    Views:
    479
    ctk70
    Feb 10, 2005
  4. Replies:
    0
    Views:
    475
  5. MattB
    Replies:
    4
    Views:
    193
    Bill Cohagan
    Sep 28, 2004
Loading...

Share This Page