Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have written code for dynamic generation of textboxes and label.Refer below but i am not getting textbox.text values.It shows error like
Specified argument was out of the range of valid values.Parameter name: index
Code:

protected void ddlanswers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int count = Convert.ToInt32(ddlanswers.SelectedValue.ToString());
            
            for (int i = 0; i < count; i++)
            {
                Label lbl = new Label();
                TextBox txt = new TextBox();
                lbl.Text = "Option " + (i + 1).ToString();
                lbl.ID = "Option" + (i + 1).ToString();
                
                txt.ID = i.ToString();
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                cell.Controls.Add(lbl);
                row.Cells.Add(cell);
                TableCell cell1 = new TableCell();
                cell1.Controls.Add(txt);
                row.Cells.Add(cell1);
                Table1.Rows.Add(row);            
         }           
        }
protected void btnsavequestion_Click(object sender, EventArgs e)
        {
            int count1 = Convert.ToInt32(ddlanswers.SelectedValue.ToString());             
            for (int i = 0; i < count1; i++)
            {
                TextBox tb = Table1.Rows[i].Cells[1].FindControl(i.ToString()) as TextBox;
     //I m getting error here
//error:Specified argument was out of the range of valid values.Parameter name: index           
                
            }
        }


[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 28-May-11 2:05am
v2

1 solution

You need to find out what is going on: Certainly, the problem is that "i" is getting to big for the number of rows in "Table1", but you need to look at the value of "count1" and work out why it is bigger than the number of rows.

Put a breakpoint on the line
int count1 = Convert.ToInt32(ddlanswers.SelectedValue.ToString());
And follow it through from there.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900