简体   繁体   中英

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 please any one can tell me where am wrong

You have different table cells for each control you are adding.

tc has the checkbox control tc1 has the label control

EDIT: A few other changes were needed to your code.

  1. You were iterating through, tbl.Controls not tbl.Rows
  2. You were iterating through tr.Controls not 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM