Fetching value from textbox dyanamically
-
I have a web form with a table control. No. of columns & no. of rows are asked by user and table is generated with cells filled with textboxes dynamically. The id of textboxes are generated dynamically. User enters the values in textboxes in table & click calculate button, on which I want to supply a set of results based on input data. Can anybody tell me how to fetch the value entered in a textbox at runtime. I am using following code. public void CreateTableRawData(int rows) { for (int i = 0; i < rows; i++) { TableRow tr = new TableRow(); TableCell td = new TableCell(); TextBox txt = new TextBox(); td.Controls.Add(txt); txt.ID = "txt" + i.ToString(); tr.Cells.Add(td); Table1.Rows.Add(tr); Table1.Caption = "Raw Data Form"; } } protected void Button2_Click(object sender, EventArgs e) { int rows = Convert.ToInt32(TextBox_rows.Text.ToString()); double[] data = new double[rows]; string [] dataText = new string[rows]; Table tbl = (Table)Panel1.FindControl("Table1"); for (int i = 0; i < rows; i++) { data[i] = Convert.ToDouble(Table1.Rows[i].Cells[0].Text.ToString()); } data[0] = Convert.ToDouble(Table1.Rows[0].Cells[0].ToString()); Label lbl = new Label(); double sum = DistributionFitting.Statistics.Sum(data); lbl.Text = sum.ToString(); } I am getting following error at runtime. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
-
I have a web form with a table control. No. of columns & no. of rows are asked by user and table is generated with cells filled with textboxes dynamically. The id of textboxes are generated dynamically. User enters the values in textboxes in table & click calculate button, on which I want to supply a set of results based on input data. Can anybody tell me how to fetch the value entered in a textbox at runtime. I am using following code. public void CreateTableRawData(int rows) { for (int i = 0; i < rows; i++) { TableRow tr = new TableRow(); TableCell td = new TableCell(); TextBox txt = new TextBox(); td.Controls.Add(txt); txt.ID = "txt" + i.ToString(); tr.Cells.Add(td); Table1.Rows.Add(tr); Table1.Caption = "Raw Data Form"; } } protected void Button2_Click(object sender, EventArgs e) { int rows = Convert.ToInt32(TextBox_rows.Text.ToString()); double[] data = new double[rows]; string [] dataText = new string[rows]; Table tbl = (Table)Panel1.FindControl("Table1"); for (int i = 0; i < rows; i++) { data[i] = Convert.ToDouble(Table1.Rows[i].Cells[0].Text.ToString()); } data[0] = Convert.ToDouble(Table1.Rows[0].Cells[0].ToString()); Label lbl = new Label(); double sum = DistributionFitting.Statistics.Sum(data); lbl.Text = sum.ToString(); } I am getting following error at runtime. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
ashutosh_karna wrote:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
As exception shows,you must be accessing some array's index that doesn't hold.Debug it ,you'll be able to get the problem
Cheers!! Brij Check My Latest Article
-
I have a web form with a table control. No. of columns & no. of rows are asked by user and table is generated with cells filled with textboxes dynamically. The id of textboxes are generated dynamically. User enters the values in textboxes in table & click calculate button, on which I want to supply a set of results based on input data. Can anybody tell me how to fetch the value entered in a textbox at runtime. I am using following code. public void CreateTableRawData(int rows) { for (int i = 0; i < rows; i++) { TableRow tr = new TableRow(); TableCell td = new TableCell(); TextBox txt = new TextBox(); td.Controls.Add(txt); txt.ID = "txt" + i.ToString(); tr.Cells.Add(td); Table1.Rows.Add(tr); Table1.Caption = "Raw Data Form"; } } protected void Button2_Click(object sender, EventArgs e) { int rows = Convert.ToInt32(TextBox_rows.Text.ToString()); double[] data = new double[rows]; string [] dataText = new string[rows]; Table tbl = (Table)Panel1.FindControl("Table1"); for (int i = 0; i < rows; i++) { data[i] = Convert.ToDouble(Table1.Rows[i].Cells[0].Text.ToString()); } data[0] = Convert.ToDouble(Table1.Rows[0].Cells[0].ToString()); Label lbl = new Label(); double sum = DistributionFitting.Statistics.Sum(data); lbl.Text = sum.ToString(); } I am getting following error at runtime. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
Try to debug your application and check which line is throwing the exception. check the index of the array. One more important thing, you are creating the textboxes runtime. You need to create the textbox before
Page_load()
. Because in ASP.NET Page life cycle,ViewState Data
andPostBack data
loads just before the Pageload. So if you create any control after page load you can't restore the postback and view state data. The best option, try to create your controls inPage_PreInit()
Event.cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net View My Recent Article