Tuesday, June 23, 2015

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. 

No comments:

Post a Comment