In this article I am explaining how to add Dynamic Buttons, LinkButtons and ImageButtons in ASP.Net Web applications. Here I will explain how to dynamically add buttons and also attach event handlers dynamically
Adding Buttons to a Normal Web Page
Buttons
C#
//Button
Button btn = new Button();
btn.ID = "btn";
btn.Text = "Button";
btn.Click += new System.EventHandler(btn_Click);
this.form1.Controls.Add(btn);
VB.Net
'Button
Dim btn As New Button()
btn.ID = "btn"
btn.Text = "Button"
AddHandler btn.Click, AddressOf btn_Click
Me.form1.Controls.Add(btn)
Event Handler
C#
protected void btn_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "btn",
"<script type = 'text/javascript'>alert('Button Clicked');</script>");
}
VB.Net
Protected Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "btn", _
"<script type = 'text/javascript'>alert('Button Clicked');</script>")
End Sub
I am simply calling a JavaScript Alert when the Button is clicked so that it is verified that the event has fired
LinkButtons
Same way it is done for LinkButtons
C#
//Link Button
LinkButton lnk = new LinkButton();
lnk.ID = "lnk";
lnk.Text = "LinkButton";
lnk.Click += new System.EventHandler(lnk_Click);
this.form1.Controls.Add(lnk);
VB.Net
'Link Button
Dim lnk As New LinkButton()
lnk.ID = "lnk"
lnk.Text = "LinkButton"
AddHandler lnk.Click, AddressOf lnk_Click
Me.form1.Controls.Add(lnk)
Event Handler
C#
protected void lnk_Click(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "lnk",
"<script type = 'text/javascript'>alert('LinkButton Clicked');</script>");
}
VB.Net
Protected Sub lnk_Click(ByVal sender As Object, ByVal e As EventArgs)
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "lnk", _
"<script type = 'text/javascript'>alert('LinkButton Clicked');</script>")
End Sub
ImageButtons
Finally the ImageButtons
C#
//Image Button
ImageButton img = new ImageButton();
img.ID = "img";
img.AlternateText= "ImageButton";
img.Click += new ImageClickEventHandler(img_Click);
this.form1.Controls.Add(img);
VB.Net
'Image Button
Dim img As New ImageButton()
img.ID = "img"
img.AlternateText = "ImageButton"
AddHandler img.Click, AddressOf img_Click
Me.form1.Controls.Add(img)
Event Handler
C#
protected void img_Click(object sender, ImageClickEventArgs e)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "img",
"<script type = 'text/javascript'>alert('ImageButton Clicked');</script>");
}
VB.Net
Protected Sub img_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "img", _
"<script type = 'text/javascript'>alert('ImageButton Clicked');</script>")
End Sub
As you will notice for ImageButton you need to use ImageClickEventHandler this is the only difference from the Buttons and LinkButtons while attaching event handlers
Adding Buttons to a ContentPlaceHolder wrapped in Master Page
While dealing with Content Pages there is no form control so you need to add the dynamic controls to the ContentPlaceHolder instead.
To get the reference of the ContentPlaceHolder you need to do the following
C#
//Get the reference of ContentPlaceHolder
ContentPlaceHolder content = (ContentPlaceHolder)this.Master
.FindControl("ContentPlaceHolder1");
VB.Net
'Get the reference of ContentPlaceHolder
Dim content As ContentPlaceHolder = DirectCast(Me.Master _
.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
Buttons
Once you get the reference you can add the Button in the following way
C#
//Button
Button btn = new Button();
btn.ID = "btn";
btn.Text = "Button";
btn.Click += new System.EventHandler(btn_Click);
content.Controls.Add(btn);
VB.Net
'Button
Dim btn As New Button()
btn.ID = "btn"
btn.Text = "Button"
AddHandler btn.Click, AddressOf btn_Click
content.Controls.Add(btn)
You will notice now I am adding button to the ContentPlaceHolder rest everything remains the same for other buttons LinkButton and ImageButton.
This completes the article you can download the sample code in VB.Net and C# using the link below
Dynamicbuttons.zip (5.65 kb)