Share via


LinkButton Web Server Control

Creates a hyperlink-style button on a Web Forms page.

<asp:LinkButton id="LinkButton1" 
     Text="label"
     Command="Command"
     CommandArgument="CommandArgument"
     CausesValidation="true | false"
     OnClick="OnClickMethod"
     runat="server"/>
or
<asp:LinkButton id="LinkButton1"
     Command="Command"
     CommandArgument="CommandArgument"
     CausesValidation="true | false"
     OnClick="OnClickMethod"
     runat="server"/>
   Text
</asp:LinkButton>

Remarks

Use the LinkButton control to create a hyperlink-style button on a Web Forms page. Specify the text to display in the LinkButton control by either setting the Text property or placing the text between the opening and closing tags of the LinkButton control. You can create either a submit button or a command button.

Note   The LinkButton control has the same appearance as a HyperLink control, but has the same functionality as a Button control. Use the HyperLink control if you want to link to another Web page when the control is clicked.

A submit button does not have a command name associated with the button and simply posts the Web page back to the server. By default, a LinkButton control is a submit button. You can provide an event handler for the Click event to programmatically control the actions performed when the submit button is clicked.

By setting the CommandName property, you can associate a command name with the command button, such as Sort. This allows you to create multiple LinkButton controls on a Web page and programmatically determine which LinkButton control is clicked. You can also use the CommandArgument property with a command button to provide addition information about the command to perform, such as Ascending. You can provide an event handler for the Command event to programmatically control the actions performed when the command button is clicked.

Note   The LinkButton control renders JavaScript on the client browser. The client browser must have JavaScript enabled for this control to function properly. For more information on client script, see Client Script in Web Forms Pages

By default, page validation is performed when a LinkButton control is clicked. Page validation determines whether the input controls associated with a validation control on the page pass the validation rules specified by the validation control. If you have a LinkButton control that needs to disable this behavior, such as a reset button, set the CausesValidation property to false.

CAUTION   Text is not HTML encoded before it is displayed in the LinkButton control. This makes it possible to embed script within HTML tags in the text. If the values for the control come from user input, be sure to validate the values to help prevent security vulnerabilities.

For detailed information on the LinkButton Web server control's properties and events, see the LinkButton Class documentation.

Example

The following example demonstrates how to use a LinkButton control to display a message when the control is clicked.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub LinkButton_Click(sender As Object, e As EventArgs) 
         Label1.Text = "You clicked the link button"
      End Sub
   </script>
</head>
<body>
   <form runat="server">
      <h3>LinkButton Example</h3>
      <asp:LinkButton id="LinkButton1" 
           Text="Click Me" 
           Font-Name="Verdana" 
           Font-Size="14pt" 
           OnClick="LinkButton_Click" 
           runat="server"/>
      <p>
      <asp:Label id=Label1 runat=server />
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void LinkButton1_Click(Object sender, EventArgs e) 
      {
         Label1.Text="You clicked the link button";
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>LinkButton Example</h3>
      <asp:LinkButton id="LinkButton1"
           Text="Click Me!" 
           Font-Name="Verdana" 
           Font-Size="14pt" 
           OnClick="LinkButton1_Click" 
           runat="server"/>
      &nbsp;&nbsp;
      <asp:Label id=Label1 
           runat=server />
   </form>
</body>
</html>

See Also

Web Server Controls | LinkButton Class