C#

Monday, February 3, 2014

LinkButton Control in Asp.net Using C#.net

Introduction

       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
                                  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 theCommandName property to specify the command name.
  This allows you to create multiple LinkButton controls on a Web page and programmatically determine whichLinkButton 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.

LinkButton Controls and Validation

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 all pass the validation rules specified by the validation control. To prevent page validation from occurring, set the CausesValidation property to false.

Example:

The following example demonstrates how to create a LinkButton control that displays text in a Label control when the link is clicked.
<%@ 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>

No comments:

Post a Comment