ASP.NET LinkButton Control


LinkButton displays a hyperlink-style button control on a Web page.By default, a LinkButton control is a Submit button. You can also use the CommandArgument property with a Command button to provide additional information about the command to perform, such as specifying ascending order.

C# Code

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton1.Text = "VB.NET";
            LinkButton2.Text = "ASP.NET";
            LinkButton3.Text = "C#";

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Label1.Text = "VB.NET is clicked";
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Label1.Text = "ASP.NET is clicked";
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Label1.Text = "C# is clicked";
        }

    }
}

HTML Code

<html>
<body>
   
        <form runat="server">
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"> VB.NET </asp:LinkButton>
        <br />
        <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click"> ASP.NET </asp:LinkButton>
        <br />
        <asp:LinkButton ID="LinkButton3" runat="server" onclick="LinkButton3_Click"> C# </asp:LinkButton>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
   
</body>
</html>

Properties

Property Description
CausesValidation Specifies if a page is validated when a LinkButton control is clicked
CommandArgument Additional information about the command to perform
CommandName The command associated with the Command event
OnClientClick The name of the function to be executed when the LinkButton is clicked
PostBackUrl The URL of the page to post to from the current page when the LinkButton control is clicked
runat Specifies that the control is a server control. Must be set to "server"
Text The text on the LinkButton
ValidationGroup The group of controls for which the LinkButton control causes validation when it posts back to the server

ASP.NET Control Standard Properties

AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
For a full description, go to Web Control Standard Attributes.

ASP.NET Web Control Standard Properties

AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
For a full description, go to Web Control Standard Attributes.

Example of LinkButton Control

Declare one LinkButton control and one Label control in an .aspx file. When the user clicks on the link, the lbClick subroutine is executed. The subroutine sends the text "You clicked the LinkButton control" to the Label control.

C# Code

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;

namespace asptutorial
{
    public partial class blankpage : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            LinkButton1.Text = "Click Me !!";

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Label1.Text = "You clicked the LinkButton control";
        }

    }
}

HTML Code

<html>
<body>
   
        <form runat="server">
        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"> Click Me !!</asp:LinkButton>
        <br />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
   
</body>
</html>

LinkButton Control using JavaScript in HTML Code

<script  runat="server">
Sub linkbuttonClick(sender As Object, e As EventArgs) 
   Label1.Text="You clicked the LinkButton control"
End Sub
</script>

<html>
<body>
    
    <form runat="server">
    <asp:LinkButton Text="Click me!" OnClick="linkbuttonClick" runat="server" />
    <p><asp:Label id="Label1" runat="server" /></p>
    </form>

</body>
</html>

Output


Share this article on