Export (0) Print
Expand All

LinkButton Class

Displays a hyperlink style button control on a Web page.

For a list of all members of this type, see LinkButton Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.LinkButton

[Visual Basic]
Public Class LinkButton
   Inherits WebControl
   Implements IPostBackEventHandler
[C#]
public class LinkButton : WebControl, IPostBackEventHandler
[C++]
public __gc class LinkButton : public WebControl,
   IPostBackEventHandler
[JScript]
public class LinkButton extends WebControl implements
   IPostBackEventHandler

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Use the LinkButton control to create a hyperlink style button on the Web page. The LinkButton control has the same appearance as a HyperLink control, but has the same functionality as a Button control. If you want to link to another Web page when the control is clicked, consider using the HyperLink control.

Note   The LinkButton control renders JavaScript to 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.

You can create either a submit button or a command button. A submit button does not have a command name associated with it. The button 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. On the other hand, a command button has a command name associated with the button, such as Sort. Set the CommandName property to specify the command name. 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 additional information about the command to perform, such as specifying ascending order. You can also provide an event handler for the Command event to programmatically control the action performed when the command button is clicked.

CAUTION   This control can be used to display user input, which might include malicious client script. Check any information that is sent from a client for executable script, SQL statements, or other code before displaying it in your application. ASP.NET provides an input request validation feature to block script and HTML in user input. Validation server controls are also provided to assess user input. For more information, see Validation Server Controls.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to create a LinkButton control that displays text in a Label control when the link is clicked.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>

   <script language="VB" 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 language="C#" runat="server">
 
      void LinkButton_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="LinkButton_Click" 
           runat="server"/>
         
      <p>
 
      <asp:Label id=Label1 runat=server />
         
   </form>
 
</body>
</html>


[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>
<html>
<head>

   <script language="JSCRIPT" runat="server">
 
      function LinkButton_Click(sender : Object, e : EventArgs){
         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="LinkButton_Click" 
           runat="server"/>
         
      <p>
 
      <asp:Label id=Label1 runat=server />
         
   </form>
 
</body>
</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

LinkButton Members | System.Web.UI.WebControls Namespace | Button | ImageButton | HyperLink | Client Script in Web Forms Pages

Show:
© 2014 Microsoft