簡體   English   中英

指定的參數超出有效值范圍。 參數名稱:動態生成的控件上的索引

[英]Specified argument was out of the range of valid values. Parameter name: index on dynamically generated Control

我在“加載頁面”上創建動態復選框和文本框“控件”,我想在單擊按鈕時獲取控件的值,但我們無法找出

這是我的動態控制代碼

DataTable dt = new DataTable();
    da.Fill(dt);
    var Count = dt.Rows.Count;
    if (Count > 0)
    {


        TableHeaderRow thr = new TableHeaderRow();
        TableHeaderCell cellheader = new TableHeaderCell();
        TableHeaderCell thc = new TableHeaderCell();
        TableHeaderCell thcode = new TableHeaderCell();
        TableHeaderCell thcReason = new TableHeaderCell();
        thc.Text = "Select";
        thc.CssClass = "pd";
        thcode.Text = "Description";
        thcode.CssClass = "pdlbl";
        thcReason.Text = "Reason";
        thcReason.CssClass = "thcReason";
        thr.Cells.Add(thc);
        thr.Cells.Add(thcode);
        thr.Cells.Add(thcReason);
        tbl.Rows.Add(thr);

        for (int i = 0; i < Count; i++)
        {
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            CheckBox chk = new CheckBox();
            Label lbl = new Label();
            TextBox txtBox = new TextBox();
            txtBox.ID = "txt" + i.ToString();
            txtBox.TextMode = TextBoxMode.MultiLine;
            chk.ID = "chk" + i.ToString();
            lbl.ID = "lbl" + i.ToString();
            lbl.Text = dt.Rows[i]["DESCRIPTION"].ToString();
            tc.Controls.Add(chk);
            tc.Width = new Unit("5px");
            tc.CssClass = "chkcntrl";
            tc1.Controls.Add(lbl);
            tc1.Width = new Unit("75%");
            tc2.Controls.Add(txtBox);
            tc2.Width = new Unit("20%");
            tr.Cells.Add(tc);
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            tbl.Rows.Add(tr);
        }

        tbl.EnableViewState = true;
        ViewState["tbl"] = true;
    }

創建控件后,我想找出值

我的代碼在下面

foreach (TableRow tr in tbl.Controls)
    {
        string str_Confirmed = string.Empty;
        string str_Description = string.Empty;
        string str_Reason = string.Empty;
        foreach (TableCell tc in tr.Controls)
        {

            if (tc.Controls[0] is CheckBox)
            {
                if (((CheckBox)tc.Controls[0]).Checked == true)
                {
                    str_Confirmed = "Y";
                }
                else
                {
                    str_Confirmed = "N";
                }
            }
            if (tc.Controls[1] is Label)
            {
                string txt = string.Empty;
                str_Description = ((Label)tc.Controls[1]).Text;
            }
            if (tc.Controls[2] is TextBox)
            {

                str_Reason = ((TextBox)tc.Controls[2]).Text;
                //Response.Write(((TextBox)tc.Controls[0]).Text);
            }

}

但是當我執行此代碼時,我們得到的Specified argument was out of the range of valid values. Parameter name: index Specified argument was out of the range of valid values. Parameter name: index任何人都可以告訴我哪里錯了

您要添加的每個控件都有不同的表格單元格。

tc具有復選框控件tc1具有標簽控件

編輯:您的代碼需要一些其他更改。

  1. 您正在遍歷tbl.Controls而不是tbl.Rows
  2. 您正在遍歷tr.Controls而不是tr.Cells
  3. 您添加的第一行表是沒有控件的標題信息,因此添加時請檢查controls.count()是否大於0

希望這可以幫助

foreach (TableRow tr in tbl.Rows)
        {
            string str_Confirmed = string.Empty;
            string str_Description = string.Empty;
            string str_Reason = string.Empty;
            foreach (TableCell tc in tr.Cells)
            {
                if (tc.Controls.Count > 0)
                {
                    if (tc.Controls[0] is CheckBox)
                    {
                        if (((CheckBox)tc.Controls[0]).Checked == true)
                        {
                            str_Confirmed = "Y";
                        }
                        else
                        {
                            str_Confirmed = "N";
                        }
                    }
                    else if (tc.Controls[0] is Label)
                    {
                        string txt = string.Empty;
                        str_Description = ((Label)tc.Controls[1]).Text;
                    }
                    else if (tc.Controls[0] is TextBox)
                    {

                        str_Reason = ((TextBox)tc.Controls[2]).Text;
                        //Response.Write(((TextBox)tc.Controls[0]).Text);
                    }
                }
            }
        }

暫無
暫無

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

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