Calling Server Side Function From Link Button in ASP.NET

This article explains on how to execute server-side functions when the link button is clicked.

Example projects are created using Visual Studio 2008 and the project type created is Web Application.


Calling a server-side function and passing the property value.

Here is my UI code

I have added a Grid View control of ASP.NET to display a list of products. The columns displayed are Product Name and Description.

To display the Product Name, I have added a Link Button control which has the text value of the Product Name. I have added one custom property called "myCustomID" and set the property to the value of the Product ID.

Note: You can add many custom properties in controls and can access them wherever needed though you will not see them when using the Intellisense while defining them.

I am going to add a div tag to the UI that we used in the previous article "Calling JavaScript function from Link Button in ASP.NET"

The following describes how my UI body looks.

We can set the value of the Command Argument for the Link Button to be accessed on server-side. We can specify the server-side method by specifying the OnCommand event handler.

    <form id="form1" runat="server">

asp:GridView ID="ShowProducts" runat="server" AutoGenerateColumns="false">
                <asp:TemplateField HeaderText="Product Name">


                        Text='<%# Eval("Name")%>'></asp:LinkButton>

                         <asp:BoundField DataField="Description" HeaderText="Description" />
    <div id="displayDetails" runat="server">
    <asp:Label ID="ShowDetails" runat="server"></asp:Label>


We are going to use the same server-side code with the new methods.

 /// <summary>
        /// Page Load Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
            List<Product> ProductList = new List<Product>

Product(){Name="Product1", ID="1",Description = "Description1"},
Product(){Name="Product2", ID="2",Description = "Description2"},
Product(){Name="Product3", ID="3",Description = "Description3"}

            ShowProducts.DataSource = ProductList;
        /// <summary>
        /// OnCommand Method for Link Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Product_Command(Object sender, CommandEventArgs e)
            //Get the Product ID from the Command Argument
            String productID = e.CommandArgument.ToString();

            //Call the method to display the selected product ID

        /// <summary>
        /// Show the product id selected
        /// </summary>
        /// <param name="productID"></param>
        protected void ShowDetailsOfProduct(string productID)
            StringBuilder sbreport = new StringBuilder();
            sbreport.Append("<table width='95%' border='0' cellpadding='0' cellspacing='0' align='center'>");
            sbreport.Append("<tr class='tdcolbg'>");
            sbreport.Append("You have clicked :" + productID);
            ShowDetails.Text = sbreport.ToString();


Now run the application and check. If you want you can use a break point to understand the flow.

When you click on the Product Name Link, it should execute server-side code and display the information in the Label control in the Div tag.



This article discusses on how to invoke server-side code when the Link Button is clicked in the ASPX file.

In my next article we will see how to execute a server-side function through AJAX and jQuery. In today's world customers would usually like to see the application to access the server-side code from client code so that the page will not do a post back and also to reduce the performance issues. Since obtaining data from the server and a post back of the entire page is avoided it is always a best practice to go for AJAX.

Up Next
    Ebook Download
    View all
    View all