NomNomNom
NomNomNom

Reputation: 871

Generating Text box control from behind "C#"

i am kinda new to asp.net, and also my english not really good, anyway i hope you still can get my point. nah i have a question here, basically i trying to integrate my app with LinkedIn, so i am using REST API, when i click a button it redirect my page to request data from LinkedIn, it returned XML data, and it contain user's education data, the count of user's education data is uncertain, so i decided to generate text box control from behind ("C#"). i do this :

            TextBox txt;
            int i;

            foreach (var element in person)
            {
                if ((element.Name == "first-name") || (element.Name == "last-name"))
                {
                    tbName.Text = tbName.Text + " " + element.Value;
                }
                else if (element.Name == "skills")
                {
                    i = 1;

                    foreach (var child in element.Elements())
                    {
                        if (child.Name == "skill")
                        {
                            txt = new TextBox();
                            txt.ID = "tbSkills" + i;
                            txt.Width = 200;
                            txt.Visible = true;
                            txt.ReadOnly = true;
                            txt.Text = child.Element("skill").Element("name").Value;
                            form1.FindControl("divMoreSkills").Controls.Add(txt);
                            i++;
                        }
                    }
                }
                else if (element.Name == "industry")
                {
                    tbIndustry.Text = element.Value;
                }
                else if (element.Name == "educations")
                {
                    i = 1;

                    foreach (var child in element.Elements())
                    {
                        if (child.Name == "education")
                        {

                            txt = new TextBox();
                            txt.ID = "tbEducations" + i;
                            txt.Width = 200;
                            txt.Visible = true;
                            txt.ReadOnly = true;
                            txt.Text = child.Element("school-name").Value;
                            form1.FindControl("divMoreEducations").Controls.Add(txt);
                            i++;
                        }
                    }
                }

            }
        }

my question is, if i want to use text box i generated previously later, does C# will recognize it? because the control i generated did not have runat server property.

thank you.

Upvotes: 2

Views: 281

Answers (2)

Habib
Habib

Reputation: 223277

if i want to use text box i generated previously later, does C# will recognize it? because the control i generated did not have runat server property.

You don't have to specify runat="server", since from the code behind you will be creating server side controls. runat="server" is used on the aspx pages to identify server side controls.

To find it, you need to make sure that these controls are available on the post back. you can find them like you are finding your div in form1. Use Page.FindControl

Upvotes: 2

hkutluay
hkutluay

Reputation: 6944

Yes it will.. You can use added control like

TextBox txt = (TextBox )Page.FindControl("tbSkills0");

Upvotes: 0

Related Questions