While
working in ASP.NET, its sometime needed to create ASP.Net control dynamically.
We can create control in code behind file by using code.
In
this article I will explain how to create controls dynamically using c# code or
vb.net code.
Dynamic controls are created on runtime and added to the page.
Create ” Label” Control:
Using C#:
Label lblname = new Label();
lblname.ID = "lblname";
lblname.Text = "Create Label :";
Using VB:
Dim lblname As New Label()
lblname.ID = "lblname"
lblname.Text = "Useranme :"
Add
the label to the form
Using C#:-
this.form1.Controls.Add(lblname);
Me.form1.Controls.Add(lblname)
Create a dynamic
Textbox
Using C#:-
TextBox txtname = new TextBox();
txtname.ID = "txtname";
Using VB:-
Dim txtname As New TextBox()
txtname.ID = "txtname"
Add
the textbox to the form
Using C#:-
this.form1.Controls.Add(txtname);
Using VB:-
Me.form1.Controls.Add(txtname)
Create a dynamic Button
Using C#:-
Button button = new Button();
button.ID = "btnsubmit";
button.Text = "Submit";
Using VB:-
Dim Button As New Button()
Button.ID = "btnsubmit"
Button.Text = "Submit"
Create
Event Handler
Using C#:-
button.Click += new System.EventHandler(btnsubmit_Click);
Using VB:-
AddHandler Button.Click, AddressOf btnsubmit_Click
Add
“Button” Control:
Using C#:-
this.form1.Controls.Add(button);
Using VB:-
Me.form1.Controls.Add(Button)
In this article we have learned
how to create and add controls dynamically. You can use above code as per your
requirement.
0 comments:
Post a Comment