Source view of LINKBUTTON.ASPX
DotNet Funda: Code Viewer
linkbutton.aspx | linkbutton.aspx.cs
Close Window  
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderHeader" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PlaceHolderForTitleAndIntro" Runat="Server">
    <table width="100%" cellpadding="2" cellspacing="0">
        <tr valign="top" class="ArticleTitle">
            <td style="padding-left:10px;" valign="middle">
                asp:LinkButton control</td>
        </tr>
        <tr>
            <td class="ArticleContents">
                LinkButton control is a control just like a Button control along with the flexibility to make it look like Hyperlink.
            </td>
        </tr>
        <tr>
            <td colspan="2">&nbsp;</td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PlaceHolderForContents" Runat="Server">
    <div class="ArticleContents">
        It implements an anchor &lt;a/&gt; tag that uses only ASP.NET postback mechanism to post the data on the server.
        Despite being a hyperlink, you can't specify the target URL.
        
        There is no <span class="DemoCP">UserSubmitBehavior</span> property like Button control with LinkButton control.
        <p>
        Its properties like <span class="DemoCP">BackColor, ForeColor, BorderColor, BorderStyle, BorderWidth, Height etc. </span>
        are implemented through style properites of &lt;a/&gt; tag. 
        You can set its Text property either by setting Text properties in the .aspx page or from server side page. (other properties can also be set from both pages)
        </p>        

        Following are some important properties that are very useful.
        <table width="100%" class="TutoPropPlaceHolder" border="1" cellpadding="2" cellspacing="1">
            <tr>
                <td class="DemoCP">CausesValidation</td>
                <td>
                    Value can be set as true/false. This indicates whether validation will be performed when a button is clicked.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">PostBackUrl</td>
                <td>
                    Indicates the URL on which the Form will be posted back.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">ValidationGroup</td>
                <td>
                    Gets or Sets the name of the validation group that the button belongs to. This is used to validate only a set of Form controls with a Button.
                </td>
            </tr>
            <tr>
                <td class="DemoCP">OnClick</td>
                <td>
                    Attach a server side method that will fire when button will be clicked.
                    <asp:LinkButton ID="btnServer" runat="Server" Text="LinkButton: Activate Server Side Event" OnClick="ActivateServerSideEvent" />
                </td>
            </tr>
            <tr>
                <td class="DemoCP">OnClientClick</td>
                <td>
                    Attach a client side (javascript) method that will fire when button will be clicked.
                    <asp:LinkButton ID="btnClient" runat="Server" Text="LinkButton: Activate Client Side Method" OnClientClick="GiveAlertToUser()" UseSubmitBehavior="False" CommandName="ClientSideButton" />
                </td>
            </tr>
            
        </table>
        <!-- START - Demo Section -->
        <table class="DemoPlaceHolder" border="1" cellpadding="2" cellspacing="4">
            <tr>
                <td class="DemoTitle">
                    DEMO : Button
                </td>
                <td align="right">
                    <a class="DemoShowSource" href="../../misc/codeviewer/default.aspx?pagename=~/tutorials/controls/linkbutton.aspx" target="_blank">Show Source Code</a>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lbl" AssociatedControlID="TextBox1" runat="Server" Text="Write something and Click the button"></asp:Label>
                    <asp:TextBox ID="TextBox1" runat="Server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ra1" runat="server" ControlToValidate="TextBox1" Text="Write something into Textbox" ValidationGroup="demo"></asp:RequiredFieldValidator>
                    <asp:LinkButton ID="btnSubmit" runat="Server" OnClick="FireServerSideEvent" Text="LinkButton: Fire Server Side Event" ValidationGroup="demo" /> 
                </td>
                <td>
                    Ex. <asp:Label ID="Label1" runat="server" BackColor="Coral" ForeColor="blue" BorderColor="ActiveBorder" BorderStyle="dashed" BorderWidth="1" Height="20" Text="Example of Label Control" Width="200" ></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <pre>
                        // With OnClick event
                        &lt;asp:LinkButton ID="LinkButton1" runat="Server" Text="LinkButton: Activate Server Side Event" OnClick="ActivateServerSideEvent" /&gt;
                        
                        // With OnClientClick event and CommandName
                        &lt;asp:LinkButton ID="LinkButton2" runat="Server" Text="LinkButton: Activate Client Side Method" OnClientClick="GiveAlertToUser()" UseSubmitBehavior="False" CommandName="ClientSideButton" /&gt;
                        
                        // With OnClick & ValidationGroup
                        &lt;asp:LinkButton ID="LinkButton1" runat="Server" OnClick="FireServerSideEvent" Text="LinkButton: Fire Server Side Event" ValidationGroup="demo" /&gt;
                    </pre>
                </td>
            </tr>
        </table>
        <!-- END - Demo Section -->
        Here note that when you click "LinkButton: Fire Server Side Event" button, the form will be posted only when you have entered something into the TextBox, while other buttons are posted back without validatation.
        Here only textbox and "LinkButton: Fire Server Side Event" button have same ValidateionGroup called "demo", so this button is validating only textbox.
        
        </div>
    <br />
    
    <script language="javascript" type="text/javascript">
        function GiveAlertToUser()
        {
            alert("Hi Dear, Client side method worked.");
        }
    </script>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="PlaceHolderFooter" Runat="Server">
</asp:Content>

Go Top