Thursday 14 June 2012

LinkButton

LinkButton is used to create a hyperlink in a website.This control looks like hyperlink but it has a same functionality of hyperlink control ans a button control.You can use the LinkButton control by specifying Text to display in linkbutton control by either the Text property or specifying the text between the opening tag and closing tag.
When you click on a linkbutton control then linkbutton take you to desired webpage by using PostBackUrl property e,g, PostBackUrl="~/how-to-use-textbox.aspx".

Some Commomly Used Properties
Access Key Property:
Access Key is used to get or set the access key so that you can easily navigate to web sever controls.In other words we can say that access key is use to create a short cut key 
to web server controls.
IsEnabled Proerty:
Gets a value that indicating whether the control is active or not.
OnClickClient Property:
Gets or Sets the client side script that executes when a LinkButton's click event raised.
<%@ page language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void LinkButton1_Click (object sender, EventArgs e)
  {
    Label1.Text = "LinkButton is clicked.";
  }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
    <title>LinkButton.OnClientClick Example</title>
</head>
<body>
  <form id="form1" runat="server">

    <h3>LinkButton.OnClientClick Example</h3> 
      <br />
      <h4>Click to navigate to Microsoft.com:</h4>     
      <br />
      <asp:linkbutton id="LinkButton1"
       text="Open Web site"
       onclientclick="Navigate()"
       onclick="LinkButton1_Click"
       runat="Server" />

       <br /><br />

      <asp:label id="Label1"
        runat="Server">
      </asp:label>
    </form>
    <script type="text/javascript">
      function Navigate()
      {
        javascript:window.open("http://www.microsoft.com");
      }    

    </script>
</body>
</html>

PostBackUrl Property:
The PostBackUrl property allows you to perform a cross-page post using the LinkButton control. Set the PostBackUrl property to the URL of the Web page to post to when the LinkButton control is clicked. For example, specifying Page2.aspx causes the page that contains the LinkButton control to post to Page2.aspx. If you do not specify a value for the PostBackUrl property, the page posts back to itself.


To see more properties of LinkButton control click following link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.aspx

2 comments: