【问题标题】:Custom Checkbox list control Error in asp.net. "startIndex cannot be larger than length of string"asp.net 中的自定义复选框列表控件错误。 “startIndex 不能大于字符串的长度”
【发布时间】:2009-06-25 19:58:20
【问题描述】:

我正在创建一个自定义复选框控件,以便在复选框列表中的每个复选框之后添加一个 div。 课程如下。

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

但是当我在页面中使用它并调用保存时出现错误。错误是:

startIndex 不能大于 字符串的长度。参数名称: startIndex 描述:一个未处理的 期间发生异常 执行当前的 Web 请求。 请查看堆栈跟踪以获取更多信息 有关错误和位置的信息 它起源于代码。

异常详情: System.ArgumentOutOfRangeException: startIndex 不能大于 字符串的长度。参数名称: 开始索引

来源错误:

产生了一个未处理的异常 在当前执行期间 网络请求。有关的信息 异常的起源和位置 可以使用异常识别 下面的堆栈跟踪。

堆栈跟踪:

[ArgumentOutOfRangeException: startIndex 不能大于 字符串的长度。参数名称: 开始索引]
System.String.InternalSubStringWithChecks(Int32 startIndex,Int32 长度,布尔值 fAlwaysCopy) +7492915
System.Web.UI.WebControls.CheckBoxList.LoadPostData(字符串 postDataKey、NameValueCollection postCollection) +60
System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(字符串 postDataKey、NameValueCollection postCollection) +13
System.Web.UI.Page.ProcessPostData(NameValueCollection postData,布尔值 fBeforeLoad) +346
System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint) +1743

有什么想法吗?

【问题讨论】:

  • 我找到了解决方案,我将名称改为“ writer.WriteAttribute("name", UniqueID & "$" & repeatIndex)" jasonlinham.co.uk/2009/04/… 感谢 Jason Linham 的博客。

标签: asp.net vb.net custom-controls


【解决方案1】:

在您的覆盖 renderItem 函数声明上放置一个断点。使用 VS 调试模式启动您的项目并跟踪设置了哪些变量以及设置了哪些变量。我有一种感觉,您在某处的空字符串上使用了 1 的 startindex。请记住,数组等从 0 开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2013-05-03
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    相关资源
    最近更新 更多