0

I have added textboxes dynamically in a FlowlayoutPanel from SQL table like this

string query = "SELECT* FROM tblA";

using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=DummyData;Integrated Security=True"))
{
    conn.Open();

    using (SqlCommand cmd = new SqlCommand(query, conn))
    {
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            Label objLbl = new Label();
            TextBox objText = new TextBox();
            objLbl.Text = reader["A_Name"].ToString();
            objText.Name = "txt"+reader["ID"].ToString();
            pnlFlow.Controls.Add(objLbl);
            pnlFlow.Controls.Add(objText);
        }
    }
}

It's working fine. Now the problem that I'm facing is when user enters some data in these textboxes. How do I get that data for further processing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Polowing
  • 123
  • 1
  • 10

1 Answers1

1

There are a number of ways you could do this, depends on how and when you need to get the values.

If you need to read them all at once, something like:

foreach(Control c in pnlFlow.Controls)
{
      if c.Name.StartsWith("txt")
      // process the text box
      // you might want to use a more distinct naming pattern to be safe
      ...
}

If you need to process them individually and at different times, you could reference them by name in the Controls collection:

string textBoxName = "txt12345";
string valueText = ((TextBox)pnlFlow.Controls[textBoxName]).Text;

Of course both snippets need better error handling, but I'll leave that to you.

cdkMoose
  • 1,646
  • 19
  • 21