Add a TextBox control at runtime on the form. - ASP.NET HTML and Web Server Control

Q.  How will you add a TextBox control at runtime on the form? Choose the correct one.
- Published on 27 Jul 15

a. TextBox obj = new TextBox();
    obj.ID = "txtUserName";
    form1.Controls.Add(obj);
b. form1.Controls.Add(TextBox);
c. this.FindControl.add(TextBox);
d. None of the above.

ANSWER: TextBox obj = new TextBox();
    obj.ID = "txtUserName";
    form1.Controls.Add(obj);
 

    Discussion

  • Ramesh   -Posted on 19 Oct 15
    For creating object at runtime you should use preinit event. Write the code given below in preinit event as follows.
    TextBox obj = new TextBox();
    obj.ID = "txtUserName";
    form1.Controls.Add(obj);

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)