Get text from dynamically created textbox in asp.net

后端 未结 3 1461
无人及你
无人及你 2020-12-20 22:07

I\'ve been banging my head against this all morning, so hopefully I can get some help. Essentially I\'m having issues getting values from some textbox controls I\'m creating

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-20 22:21

    Create Dynamic TextBoxes and add it to a asp panel so that you can access it easily.

    Here is the ASP.NET design elements.

    C# Code to generate Dynamic textboxes

     protected void create_dynamic_text(object sender, EventArgs e)
    {
        int num = 5; // you can give the number here
        for (int i = 0; i < num;i++ )
        {
            TextBox tb = new TextBox();
            tb.ID = "txt_box_name" + i.ToString();
            tb.CssClass = "add classes if you need";
            tb.Width = 400; //Manage width and height 
            panel.Controls.Add(tb); //panel is my ASP.Panel object. Look above for the design code of ASP panel
        }
    }
    

    C# Code to Take Values

     protected void reade_values(object sender, EventArgs e)
    {
       int num=5; // your count goes here
        TextBox tb = new TextBox();
            for (int i = 0; i < num; i++)
            {
               tb=(TextBox)panel.FindControl("txt_box_name"+i.ToString());
               string value = tb.Text; //You have the data now
            }
        }
    }
    

提交回复
热议问题