简体   繁体   中英

How to dinamycally create runtime controls in asp.net/webforms c#?

I've a table in a database with this structure:

  • field_order

  • field_label

  • field_name

  • field_type

  • width

  • heigth

I want to loop through all rows and create controls in my page, putting them in classic table 2 columns x X rows (each row a control).

This is server side code:

 using (MySqlConnection db = new MySqlConnection(myAppConnectionString))
            {
                string sql = "SELECT * FROM myTable WHERE idtemplate =" + templateID.ToString() + " ORDER BY field_order";
                MySqlDataReader dr;
                MySqlCommand cmd = new MySqlCommand(sql,db);

                db.Open();

                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        string field_label = dr["field_label"].ToString();
                        string field_name  = dr["field_name"].ToString();
                        string field_type = dr["field_type"].ToString();
                        string field_width = dr["width"].ToString();
                        string field_height = dr["height"].ToString();

                        switch (field_type)
                        {
                            case "Text":
                                Label lbl = new Label();
                                lbl.Text = field_label;
                                lbl.ID = "lbl" + field_name;
                                panelFields.Controls.Add(lbl);

                                TextBox txt = new TextBox();
                                txt.Text = "";
                                txt.ID = "txt" + field_name;
                                Unit ut = new Unit(field_width);
                                txt.Width = ut;
                                ut = new Unit(field_height);
                                txt.Height = ut;
                                panelFields.Controls.Add(txt);


                                break;
                            case "Number":
                                break;
                            case "Date":
                                break;
                            case "Time":
                                break;
                        }
                    }
                }

            }

Using panelFields.Controls.Add(myControl) I add each controls in a panel but next to each other.

So my questions are:

1- How can I add controls in a table 2 columns x N rows ?

2- If I add controls in this way, Will it possible for me to access from server side to save TextBox values ?

Thanks

I solved adding "Literal" controls to myPanel with HTML Tag.

Like:

myPanel.Controls.Add(new LiteralControl(""));

myPanel.Controls.Add(new LiteralControl(" "));

....

myPanel.Controls.Add(new LiteralControl(""));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM