Table of contents
1.
Introduction
2.
LinkButton Syntax and Properties
2.1.
Declarative Syntax and Class
2.2.
Properties
2.3.
Methods
3.
Example to Understand LinkButton
3.1.
.aspx code
3.2.
.aspx.cs code
3.3.
Output
4.
Frequently Asked Questions
5.
Key Takeaways
Last Updated: Mar 27, 2024

Web Forms LinkButton

Author Naman Kukreja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

As you are reading this blog, you are accessing a website that means you are surfing the internet. In today's modern world, if a person is surfing on the internet, he knowingly or unknowingly uses many buttons in his everyday life. 

Here we will discuss one of the buttons, which are LinkButtons. You will learn all about LinkButton, its properties, its methods, with examples.

LinkButton Syntax and Properties

Before moving on to LinkButton’s syntax and properties, let’s first learn about LinkButton. 

As the name suggests, it is a button, but the factor that makes it different from the rest of them is its presentation style. It is a hyperlink-style button on the web page on the front end. It will look just like Hyperlink but has a function as a button.

Declarative Syntax and Class

Here, we see that ASP.NET provides a tag to create LinkButton. Here we will see the syntax of that:

<asp:LinkButton  
    AccessKey="string"  
    ID="string"  
    OnClick="Click event handler"  
    OnCommand="Command event handler"
    OnClientClick="string"  
    OnDataBinding="DataBinding event handler"  
    OnDisposed="Disposed event handler"  
    OnInit="Init event handler"  
    OnPreRender="PreRender event handler"  
    OnLoad="Load event handler"  
    OnUnload="Unload event handler"  
    PostBackUrl="uri"  
    runat="server"  
/>

Properties

These are some of the properties of the LinkButton in ASP.NET.

  1. Adapter: Gets the browser-specific adapter for the control.
  2. BackColor: Sets or gets the Web Server Control background color.
  3. ClientID: Gets the ASP.NET generated control ID for HTML markup language.
  4. Font: Gets all the font-related properties associated with Web Server Control.
  5. IsEnabled: Indicate with a value whether the control is enabled or not.
  6. Visible: Gets or sets whether a server control is rendered as UI on the page.
  7. Width: Sets the width of the Web Control Server.

Methods

Now, we will discuss some of the methods and their functions in ASP.NET.

  1. ApplyStyle(Style): Copies any nonblank element to the web control, overwriting the existing style elements of control.
  2. ClearCachedClientID(): Sets the cached ClientID value to null.
  3. DataBind(): Binds the data source to the server controls and its child controls.
  4. OnInit(EventErgs): Raises the Init event.
  5. OpenFile(String): Get a stream used to read a file.

Example to Understand LinkButton

Here, we will see both the .aspx and .aspx.cs code and the class code.

.aspx code

<%@ Page Language="C#" AutoEventWireup="true"  
CodeBehind="LinkButtonExample.aspx.cs" Inherits="LinkButtonExample.LinkButtonExample" %>  
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head>  
<body>  
    <form id="form1" runat="server">  
        <div>  
            <p>This is a LinkButton - A Hyperlink Style Button</p>  
        </div>  
        <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">CodingNinja</asp:LinkButton>  
        <p>  
            <asp:Label ID="Label1" runat="server"></asp:Label>  
        </p>  
    </form>  
</body>  
</html>

.aspx.cs code

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
namespace LinkButtonExample  
{  
    public partial class LinkButtonExample : System.Web.UI.Page  
    {  
        protected void LinkButton1_Click(object sender, EventArgs e)  
        {  
            Label1.Text = "Welcome to Coding Ninja";  
        }  
    }  
}

Output

When you click the LinkButton, the following message will be shown in the output.

Frequently Asked Questions

1. What is the feature of OnClick feature in the code of LinkButton?

Answer: It raises the click event of button control.

2. Can you specify the target Url in LinkButton?

Answer: No, you cannot specify the target Url.

3. What do you mean by CLR?

Answer: CLR refers to Common Language Runtime.

4. Name the types of server control supported by ASP.NET.

Answer: These are the following server controls supported by ASP.NET:

  • Web Server Controls
  • Validation Controls
  • User Controls
  • HTML Server Controls

Key Takeaways

In this blog, we have learned about LinkButton in ASP.NET, its properties, methods, and all these with examples.

Look at this blog here if you want to learn different frameworks like ASP.Net but are confused about identifying which framework to learn. You will get the complete list of the top 10 web development frameworks.

Live masterclass