EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

ASP.NET LinkButton

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » ASP.NET Tutorial » ASP.NET LinkButton

ASP.NET LinkButton

Introduction to ASP.NET LinkButton

LinkButton server control displays a hyperlink styled button on the web page. Why did I use the term server control? Well, because like all other server controls, ASP.NET provides its own tag for the LinkButton control which is run at the server and the generated HTML code is returned as a response to the browser.

So, thinking from an HTML perspective, the LinkButton control generates the HTML anchor (hyperlink) element. It lets the users navigate to a section within the page or to another page. The LinkButton control looks like a hyperlink on the web page and functions as a button. This makes it a very useful control.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

The LinkButton control can be coded using ASP.Net provided tags or dragged and dropped using Visual Studio IDE. The drag and drop feature ultimately generates the same code.

The syntax of ASP.Net LinkButton in its simplest form is:

<asp:LinkButton ID="<linkButtonId>" runat="server">DisplayText</asp:LinkButton>

Behind the Scenes

What happens behind the scenes? When you send a request from your browser for an ASP.Net page, the server locates the appropriate code and the ASP.Net engine starts compiling it. When the ASP.Net compiler encounters any ASP tag which is marked to be run at the server, it converts the tag into native HTML code. The generated HTML code is then sent to the browser in the response.

For example, the ASP.Net LinkButton control is coded as:

<asp:LinkButton ID="myLinkButton" PostBackUrl=”~/myWebPage.aspx” runat="server"> Click here! </asp:LinkButton>

Server renders it to the browser in the following HTML control:

Popular Course in this category
ASP.NET Training (8 Courses, 19 Projects)8 Online Courses | 19 Hands-on Projects | 105+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,995 ratings)
Course Price

View Course

Related Courses

<a id="myLinkButton" href=”www.myWebSite.com/myWebPage.aspx”> Click here! </a>

Note: The above rendered HTML is simplified for illustration and better explanation. The actual rendered HTML is a bit complex as it contains the JavaScript postback method. Read below for more explanation.

LinkButton vs HyperLink

Now you may be wondering the LinkButton is a redundant control since ASP.Net already has a much-simplified HyperLink control which does the same job. You are partially correct. Although both the LinkButton and HyperLink controls perform the same task of navigating the user from one page to another, there is a fundamental difference between both. The HyperLink control redirects the user to the target page immediately. There is no intermediate call made to the server. It is all handled by the browser.

The LinkButton control makes a postback call to the server before redirecting the user to the target page. This postback call posts the form to the server with the target URL and other code to be processed. The server processes this postback request and in its response, the user is redirected to the target page. This is very helpful when you want to execute some code in the server before navigating to the target page.

Properties of ASP.NET LinkButton

The ASP.Net LinkButton control comes with certain pre-defined properties. These properties are converted to attributes in the native HTML code. They help define additional behavior for the LinkButton control. Let’s discuss some of the frequently used ones in detail:

1. BackColor, ForeColor

This property gets or sets the background and the foreground color of the link button.

<asp:LinkButton ID="myLinkButton" BackColor="DarkBlue" ForeColor="White" runat="server" >Click here </asp:LinkButton>

2. BorderColor, BorderStyle and BorderWidth

These properties get or set the border styling for the link button control.

<asp:LinkButton ID="myLinkButton" BorderWidth="5" BorderColor="Blue" BorderStyle="dashed" runat="server" > Click here </asp:LinkButton>

3. CausesValidation

This property gets or sets a value which indicates whether a validation must be performed or not when the link button is clicked. The default is true.

<asp:LinkButton ID="myLinkButton" CausesValidation="false" runat="server" > Click here </asp:LinkButton>

4. CssClass

This property gets or sets the CSS class to be applied to the control.

<asp:LinkButton ID="myLinkButton" CssClass="txtBxClass" runat="server" > Click here </asp:LinkButton>

5. Enabled

This property gets or sets the value indicating whether the link button control is enabled or disabled. The default value is true.

<asp:LinkButtonID="myLinkButton" Enabled="false" runat="server" > Click here </asp:LinkButton>

6. Font

This property gets or sets the font of the text to be displayed in the link button. There are plenty of styles and options such as bold, italics, underline, strikeout, etc.

7. Height, Width

These properties get or set the height and width of the link button in the number of pixels.

<asp:LinkButtonID="myLinkButton" Height="100" Width="500" runat="server" > Click here </asp:LinkButton>

8. ID

This property gets or sets the unique identifier attribute to the link button.

<asp:LinkButtonID="myLinkButton" runat="server" > Click here </asp:LinkButton>

9. PostBackUrl

This property gets or sets the URL of the page to navigate to from the current page.

<asp:LinkButtonID="myLinkButton" PostBackUrl="~/mywebpage.aspx" runat="server" > Click here </asp:LinkButton>

10. Text

This property gets or sets the text displayed in the link button control.

<asp:LinkButtonID="myLinkButton" Text="Click here" runat="server" > </asp:LinkButton>

11. ToolTip

This property gets or sets the tooltip value to be displayed when the mouse pointer has hovered over the link button.

<asp:LinkButtonID="myLinkButton" ToolTip="Click here to go to my web page." runat="server" > Click here </asp:LinkButton>

12. Visible

This property determines whether the link button control will be displayed on the UI or hidden. The default is true.

<asp:LinkButtonID="myLinkButton" Visible="false" runat="server" > Click here </asp:LinkButton>

Examples of ASP.NET LinkButton

Let us create an ASP.Net webform with a link button to navigate to another ASP.Net page on the same website.

Step 1. Create a new ASP.Net web application project. This will create a shell template with a working application with a Default.aspx and About.aspx page.

Step 2. Go to the Default.aspx file and remove the contents of the shell template to look like below:

ASP.NET LinkButton-1.1

Step 3. In the Toolbox pane of Visual Studio IDE, you would notice a set of Web controls ready to drag and drop in your project. Find the LinkButton control and drag it in the Default.aspx page.

ASP.NET LinkButton-1.2

Step 4. Once you drop the LinkButton control, you would notice an auto-generated ASP.Net LinkButton tag in your Default.aspx file. Modify the code to look like below.

Default.aspx.cs file-1.3

Alternatively, you can skip the drag and drop part and write the above code yourself.

<asp:LinkButton ID="linkToAboutPage" PostBackUrl="~/About.aspx" OnClick="linkToAboutPage_Click" runat="server">About Us</asp:LinkButton>

Step 5. Copy the below code in your Default.aspx.cs file.

protected void linkToAboutPage_Click(object sender, EventArgs e)
{
// execute code before redirecting to the target page
}

Default.aspx.cs file-1.4

Step 6. Run the application. Below is the output of your code. It has a link button that redirects to the About.aspx page of the application.

The code-behind file has an OnClick() function. You can write your code to be executed before the navigation to the target page.

ASP.NET LinkButton-1.5

Click on the link and you would be redirected to the About.aspx page.

ASP.NET LinkButton-1.6

Recommended Articles

This is a guide to ASP.NET LinkButton. Here we discuss the introduction and properties of asp.net linkbutton along with differences between linkbutton vs hyperlink. You may also look at the following articles to learn more –

  1. RadioButton in ASP.NET
  2. Caching In ASP.NET
  3. ImageButton in ASP.NET?

ASP.NET Training (8 Courses, 19 Projects)

8 Online Courses

19 Hands-on Projects

105+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
ASP.NET Tutorial
  • ASP.NET Controls
    • ASP.NET Label
    • ASP.NET TextBox
    • Button in ASP.NET
    • ASP.NET CheckBoxList
    • ASP.NET DataList
    • RadioButton in ASP.NET
    • ASP.NET CheckBox
    • ASP.NET Hidden Field
    • ASP.NET LinkButton
    • ImageButton in ASP.NET
    • ASP.NET ListBox
    • Drop Down List in ASP.NET
    • ASP.NET Image
    • ASP.NET MVC ViewBag
    • ASP.NET GridView
    • Calendar in ASP.NET
    • ASP.NET Datagrid
    • ASP.NET Hyperlink
    • Timer in ASP.NET
    • ASP.NET Cookie
    • ASP.NET Session
    • ASP.NET SessionID
    • ASP.NET FileUpload
    • ASP.NET Download File
    • ASP.NET UpdatePanel
    • Authentication in ASP.NET
    • ASP.NET MVC Routing
    • ASP.NET MVC Authentication
    • ASP.NET ViewState
  • Basic
    • What is ASP.NET
    • Features of ASP.NET
    • ASP.NET Versions
    • ASP.NET Framework
    • What is MVVM
    • What is Eclipse IDE?
    • ASP.NET Page Life Cycle
    • ASP.NET Server Controls
    • Caching In ASP.NET
    • Data Binding in ASP.NET
    • What is ASP.Net Web Services
    • ASP.Net Interview Questions
    • Asp.Net MVC Interview Questions
    • AJAX Interview Questions
    • What is LINQ
    • Ajax in ASP.NET
  • ASP.Net Validation
    • ASP.Net Validation Controls
    • ASP.Net CompareValidator
    • ASP.NET RequiredFieldValidator
    • ASP.NET ValidationSummary
    • ASP.NET RegularExpressionValidator
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - ASP.NET Training (8 Courses, 19 Projects) Learn More