如何在ASP.NET中创建动态生成的文本框

时间:2015-01-29 14:22:04

标签: asp.net

我有这个代码在动态表中生成TextBoxes,我是从网站上得到的:

protected void AddStdBtn_Click(object sender, EventArgs e)
        {
            CreateDynamicTable();
        }

private void CreateDynamicTable()
        {

            // Fetch the number of Rows and Columns for the table 
            // using the properties
            if (ViewState["Stu"] != null)
                Students = (DataTable)ViewState["Stu"];
            int tblCols = 1;
            int tblRows = Int32.Parse(NoTxt.Text); 
            // Now iterate through the table and add your controls 
            for (int i = 0; i < tblRows; i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();
                    TextBox txtBox = new TextBox();
                    txtBox.ID = "txt-" + i.ToString() + "-" + j.ToString();
                    txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
                    // Add the control to the TableCell
                    tc.Controls.Add(txtBox);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);

                }
                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
                tbl.EnableViewState = true;
                ViewState["tbl"] = true;
            }
        }

        protected void CalculateBtn_Click(object sender, EventArgs e)
        {
            foreach (TableRow tr in tbl.Controls)
            {
                foreach (TableCell tc in tr.Controls)
                {

                    if (tc.Controls[0] is TextBox)
                    {
                        Response.Write(((TextBox)tc.Controls[0]).Text);
                    }
                }
                Response.Write("<br/>");
            }
        }


        protected override object SaveViewState()
        {
            object[] newViewState = new object[2];

            List<string> txtValues = new List<string>();

            foreach (TableRow row in tbl.Controls)
            {
                foreach (TableCell cell in row.Controls)
                {
                    if (cell.Controls[0] is TextBox)
                    {
                        txtValues.Add(((TextBox)cell.Controls[0]).Text);
                    }
                }
            }

            newViewState[0] = txtValues.ToArray();
            newViewState[1] = base.SaveViewState();
            return newViewState;
        }
        protected override void LoadViewState(object savedState)
        {
            //if we can identify the custom view state as defined in the override for SaveViewState
            if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
            {
                object[] newViewState = (object[])savedState;
                string[] txtValues = (string[])(newViewState[0]);

                if (ViewState["Stu"] != null)
                    Students = (DataTable)ViewState["Stu"];

                if (txtValues.Length > 0)
                {
                    //re-load tables
                    CreateDynamicTable();
                    int i = 0;
                    foreach (TableRow row in tbl.Controls)
                    {
                        foreach (TableCell cell in row.Controls)
                        {
                            if (cell.Controls[0] is TextBox && i < txtValues.Length)
                            {
                                ((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();

                            }
                        }
                    }
                }
                //load the ViewState normally
                base.LoadViewState(newViewState[1]);
            }
            else
            {
                base.LoadViewState(savedState);
            }
        }

现在,如果你检查CreateDynamicTable()函数,特别是行:

        int tblRows = Int32.Parse(NoTxt.Text); 

您可以看到用户可以通过在TextBox NoTxt中输入一个数字来控制行数。当我运行此代码时,我收到以下错误:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: Input string was not in a correct format.

,错误位置在上面的同一行。

问题是:如何让用户控制表中的行数?

1 个答案:

答案 0 :(得分:0)

经过几天的阅读和研究,可以使用Session变量完成解决方案。将以NoTxt.Text中获得的值存储如下:

Session["V"] = Int32.Parse(NoTxt.Text); 

并在需要时检索它。