简体   繁体   English

指定的参数超出有效值范围。 参数名称:动态生成的控件上的索引

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

i'm creating dynamic checkbox and textbox Control on page Load and i want to get the values of control on button click but we are not able to find out 我在“加载页面”上创建动态复选框和文本框“控件”,我想在单击按钮时获取控件的值,但我们无法找出

here is my Dynamic control Code 这是我的动态控制代码

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;
    }

after create the control i want to find out the values 创建控件后,我想找出值

my code are below 我的代码在下面

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);
            }

} }

but when i will execute this code we are getting the 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 Specified argument was out of the range of valid values. Parameter name: index please any one can tell me where am wrong Specified argument was out of the range of valid values. Parameter name: index任何人都可以告诉我哪里错了

You have different table cells for each control you are adding. 您要添加的每个控件都有不同的表格单元格。

tc has the checkbox control tc1 has the label control tc具有复选框控件tc1具有标签控件

EDIT: A few other changes were needed to your code. 编辑:您的代码需要一些其他更改。

  1. You were iterating through, tbl.Controls not tbl.Rows 您正在遍历tbl.Controls而不是tbl.Rows
  2. You were iterating through tr.Controls not tr.Cells 您正在遍历tr.Controls而不是tr.Cells
  3. The first table row you add is the header information that has no controls so added check that controls.count() is greater than 0 您添加的第一行表是没有控件的标题信息,因此添加时请检查controls.count()是否大于0

Hope this helps 希望这可以帮助

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.

相关问题 异常:指定的参数超出有效值的范围。 (参数“索引”) - Exception: Specified argument was out of the range of valid values. (Parameter 'index') 指定的参数超出了有效值的范围。 参数名称:值 - Specified argument was out of the range of valid values. Parameter name: value 指定的参数超出有效值范围。 参数名称:大小 - Specified argument was out of the range of valid values. Parameter name: size 得到“指定的参数超出有效值范围。 参数名称:” - Getting “Specified argument was out of the range of valid values. Parameter name:” 指定的参数超出有效值范围。 参数名称:site - Specified argument was out of the range of valid values. Parameter name: site 指定的参数超出有效值范围。 参数名称索引。 由ObservableCollection.Add()方法抛出 - Specified argument was out of the range of valid values. parameter name index. thrown by ObservableCollection.Add() method 重现异常:指定的参数超出有效值范围。 参数名称:索引 - Reproduce exception: Specified argument was out of the range of valid values. Parameter name: index 错误指定的参数超出了有效值的范围。 参数名称:DataGridview Row Data Bound 中的索引 - Error Specified argument was out of the range of valid values. Parameter name: index in DataGridview Row Data Bound System.ArgumentOutOfRangeException: &#39;指定的参数超出了有效值的范围。 参数名称:asp.net中的index&#39; - System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: index' in asp.net FastMember:指定的参数超出有效值范围。 参数名称:名称 - FastMember: Specified argument was out of the range of valid values. Parameter name: name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM