Asp.net :Create controls dynamically - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Asp.net :Create controls dynamically

Introduction: In this article I am going to explain how to create controls dynamically in asp.net

Description:

Dynamic controls are created at runtime and added to the page.

Create a dynamic label
C#:
  Label lblname = new Label();
  lblname.ID = "lblname";
  lblname.Text = "Useranme :";
VB:
Dim lblname As New Label()
lblname.ID = "lblname"
lblname.Text = "Useranme :"

Add the label to the form
C#:-
this.form1.Controls.Add(lblname); 
VB:-
Me.form1.Controls.Add(lblname)

Create a dynamic Textbox
C#:-
TextBox txtname = new TextBox();
txtname.ID = "txtname";
VB:-
Dim txtname As New TextBox()
txtname.ID = "txtname"

Add the textbox to the form
C#:-
this.form1.Controls.Add(txtname);
VB:-
Me.form1.Controls.Add(txtname)

Create a dynamic Button
C#:-
Button button = new Button();
button.ID = "btnsubmit";
button.Text = "Submit";
VB:-
Dim Button As New Button()
Button.ID = "btnsubmit"
Button.Text = "Submit"

Create an Event handler
C#:-
button.Click += new System.EventHandler(btnsubmit_Click);
VB:-
AddHandler Button.Click, AddressOf btnsubmit_Click

Add the Button to the form:
C#:-
this.form1.Controls.Add(button);
VB:-
Me.form1.Controls.Add(Button)

In this article we have learn how to create controls dynamically in asp.net (C#,VB). I hope you enjoyed this article. 

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon