This tutorial will show how to create a LinkButton control in Visual Basic.NET. The LinkButton control has the same appearance as a HyperLink control, but functions like a Button control. Consider the LinkButton like a submit button, code can be provided for it in the code behind when clicked unlike a HyperLink control. In this tutorial we will show how to add code in the button when it is clicked and how to redirect to another page just as a HyperLink control would do.

Setting Up

If you have not done so already, create a new web site. To do so, open Visual Studio and click File > New > Web Site. Using ‘Visual Basic’ as the template, select ASP.NET Empty Web Site, name the file LinkButtonControl-VB, and click OK. Now right click the web site name and click Add New Item. Again, using ‘Visual Basic’ as the template, select Web Form, leave the file name as Default.aspx and click add.

Example One

This example will show how to use the LinkButton control like a button. In the source view of Default.aspx, place a LinkButton and Literal control inside the div tags. Change “LinkButton” to “Submit” so it will show Submit on the web page.

Now switch to design view and double click the LinkButton to generate an event in the code behind. When clicked we want to set the Literal control text to “You Clicked the LinkButton Control”.

Output

Run the page and click the LinkButton control with the text “Submit”. When it is clicked the Literal control outputs the message “You Clicked the LinkButton Control”. This is an example of creating code in the event that it is clicked, which a HyperLink control cannot do.

linkbutton 1

Example Two

This tutorial will show how to redirect to another page using a LinkButton control. Open Default.aspx in source view and place a LinkButton control below the Literal control. Change “LinkButton” to “Redirect Page”.

Now switch to design view and double click the Redirect Page link to generate an event in the code behind. In the LInkButton2_Click event method, we will use the Response.Redirect method to redirect the page to ‘v4.vbasic.net’.

Output

Run the page and you should see a “Redirect Page” LinkButton, click it. It will redirect you to our v4.vbasic.net page. Even though it doesn’t traditionally redirect to another page on click like a HyperLink control would, we are able to write code to do so in the code behind. Being able to write click events for the LinkButton makes it more powerful than a HyperLink, allowing more options for the programmer.

Thanks for reading and make sure to download the source files to get a better understanding of how the code works.

Download Source Files