asp.net - How to use LinkButton click event

Click event in LinkButton control
LinkButton is an asp.net web server control. the linkbutton server control render a hyperlink-style button control on web page. though it look likes a hyperlink but it has the same functionality as a button control. by default, a linkbutton control is a submit button. we also can use it as a command button.

when we use linkbutton control as a regular submit button, we can provide an event handler for its Click event. submit button simply post the web page back to the server. this Click event allow us to programmatically control the action performed the submit button (linkbutton) is clicked.

linkbutton Click event occurs when the linkbutton control is clicked. the click event is commonly used when no command name is associated with the linkbutton control.

linkbutton OnClick method raises the Click event of the linkbutton control. the OnClick method required a parameter named e. this parameter type is System.EventArgs that contains the event data.

the following asp.net c# example code demonstrate us how can we use linkbutton click event in an asp.net application.
LinkButtonOnClick.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void LinkButton1_Click(object sender, System.EventArgs e)
    {
        Label1.Text = "Your Address:<br />" + TextBox1.Text;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use OnClick event in LinkButton control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">LinkButton Example: OnClick Event</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             Font-Bold="true"
             ForeColor="HotPink"
             Font-Italic="true"
             Font-Size="Large"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             Text="Address"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1"
             runat="server"
             BackColor="LightGoldenrodYellow"
             ForeColor="DodgerBlue"
             TextMode="MultiLine"
             >
        </asp:TextBox>
        <br /><br />
        <asp:LinkButton 
             ID="LinkButton1" 
             runat="server"
             Text="Submit Address"
             OnClick="LinkButton1_Click"
             ForeColor="Crimson"
             Font-Size="Large"
             BorderWidth="2"
             >
        </asp:LinkButton>
    </div>
    </form>
</body>
</html>








Related asp.net example