簡體   English   中英

自定義CheckBoxList WebControl錯誤

[英]Custom CheckBoxList WebControl Error

我正在創建一個自定義CheckBox WebControl,它繼承自ASP.NET中的System.Web.UI.WebControls.CheckBox。 但是在回發時出現此錯誤:

輸入的字符串格式不正確。

[FormatException: Input string was not in a correct format.]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +10896279
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +145
System.Web.UI.WebControls.CheckBoxList.LoadPostData(String postDataKey, NameValueCollection postCollection) +140
System.Web.UI.WebControls.CheckBoxList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +16
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +734
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1960

我有一個名為ItemList和OnInit的CheckBox列表,我執行此功能:

 internal void InitItems()
    {
        for (int i = 0; i < this.Items.Count; i++)
        {
            CheckBox cb = new CheckBox()
            {
                LabelPosition = CheckBox.LabelPositions.None
            };

            ItemList.Add(cb);
            Controls.Add(cb);
        }
    }

我將此方法稱為OnRender:

public string RenderItems() { 

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < this.Items.Count; i++)
        {
            ListItem item = this.Items[i];
            CheckBox cb = ItemList[i];
            cb.Text = item.Text;
            cb.Enabled = Enabled ? item.Enabled : Enabled;
            cb.Checked = item.Selected;
            cb.ID =  i.ToString();
            cb.ViewType = (CheckBox.ViewTypes) Enum.Parse(typeof(CheckBox.ViewTypes),this.ViewType.ToString());
            cb.DisplayType = (CheckBox.DisplayTypes)Enum.Parse(typeof(CheckBox.DisplayTypes), this.DisplayType.ToString());
            if (this.AutoPostBack)
                cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack(\\'" + cb.ClientName + "\\',\\'\\')', 0)");

            sb.Append(cb.GetHtml());
        }

        return sb.ToString();
    }

我猜我正在收到此錯誤,因為生成的復選框具有錯誤的ID或名稱 有什么建議嗎?

我想到的唯一可能原因是此行:

cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack(\\'" + cb.ClientName + "\\',\\'\\')', 0)");

您不需要那些\\\\

cb.Attributes.Add("onclick", "javascript:setTimeout('__doPostBack('" + cb.ClientName + "','')', 0)");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM