LinkButton Control in Asp.net C# Web Form

Introduction to LinkButton Control:

LinkButton: ImageButton displays a button with a link (instead of text) that can be pressed or clicked by the user.

Now the first question is:
Why use a LinkButton, instead of directly using a link?

You cannot directly write a logic in a link and then redirect the user when it fulfills specific condition. But you can do so with LinkButton, it means that you can display it as a link and make it work like a button.

1. Create a new web form.

2. Drag a LinkButton from toolbox on your page.

Properties:

a. ID: Set its ID.

b. AutoPostBack: Button is a control which has its AutoPostBack property set to true by default.

c. OnClientClick: You can write any JavaScript function here that will be called before postback.

d. Text: Set the text of your link button by either using text property or write between starting and ending tags. Anything that is written between its starting and ending tags will be the part of the link.

e. You can its style properties like CssClass, ForeColor, BackColor, BorderStyle, BorderColor, Height, Width, Font-Bold, Font-Size etc.

    <form id="form1" runat="server">
        <div style="align-content: center; text-align: center; font-size: large">
            <br />
            <br />
            <br />
            <br />
            <br />

            <asp:LinkButton ID="lnkBtn" runat="server"
                OnClientClick="confirm('Do you really want to go to CS Programming Tutorial Blog?')" ForeColor="#669900"
                BackColor="#66ccff" Height="100" Width="500" BorderColor="#000000" BorderStyle="Dashed" Font-Bold="true"
                Font-Size="Large" OnClick="lnkBtn_Click">
            Hi! This is a link button.
            You can click here to go redirect your page to CS Programming Tutorial Blog.
            Thanks :)
            </asp:LinkButton>

            <br />
            <br />
            <label>
                The PostBack is created by:
            </label>
            <asp:Label ID="lblMsg" runat="server"></asp:Label>
        </div>
    </form>

Functions:

Most common function of button is click, generate an onclick function by double clicking the button in design view.
Now in the function generated in .aspx.cs file you can program your logic.

Response.Write allow us to write something on top of page from backend.

You can call other controls in this function and can display data.

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void lnkBtn_Click(object sender, EventArgs e)
        {
            string msg = lnkBtn.ID.ToString() + " with Fore-Color " + lnkBtn.ForeColor.ToString() + " and "
                + lnkBtn.BackColor.ToString() + "!";
            Response.Write("<div style='align-content:center; text-align:center; font-size:large'><br /><br /><br />You clicked a link button.</div>");
            lblMsg.Text = msg;
        }

Done!
Run the program.
Click the ImageButton, first client click function will work and then backend file function will work.
 
 

Let me know in the comment section if you have any question.

Previous Post:
ImageButton Control in asp.net C# Web Form
Next Post: