LinkButton Web Server Control Declarative Syntax
Creates a hyperlink-style button on a Web Forms page.
<asp:LinkButton
AccessKey="string"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
Inset|Outset"
BorderWidth="size"
CausesValidation="True|False"
CommandArgument="string"
CommandName="string"
CssClass="string"
Enabled="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Names="string"
Font-Overline="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
Large|X-Large|XX-Large"
Font-Strikeout="True|False"
Font-Underline="True|False"
ForeColor="color name|#dddddd"
Height="size"
ID="string"
OnClick="Click event handler"
OnClientClick="string"
OnCommand="Command event handler"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
PostBackUrl="uri"
runat="server"
SkinID="string"
Style="string"
TabIndex="integer"
Text="string"
ToolTip="string"
ValidationGroup="string"
Visible="True|False"
Width="size"
/>
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 ASP.NET Web 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. |
Command buttons (such as LinkButton controls) are frequently used in templated controls to communicate an action to the parent control. For example, selection in a DataList or changing mode in a FormView.
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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LinkButton Example</title>
<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 id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LinkButton Example</title>
<script language="C#" runat="server">
void LinkButton_Click(Object sender, EventArgs e)
{
Label1.Text="You clicked the link button";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>LinkButton Example</title>
<script language="JSCRIPT" runat="server">
function LinkButton_Click(sender : Object, e : EventArgs){
Label1.Text = "You clicked the link button"
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>LinkButton Example</h3>
<asp:LinkButton id="LinkButton1"
Text="Click Me"
Font-Names="Verdana"
Font-Size="14pt"
OnClick="LinkButton_Click"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server" />
</form>
</body>
</html>