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.

Purpose

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 that 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.

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.     <div>  
  4. <asp:GridView ID="ShowProducts" runat="server" AutoGenerateColumns="false">  
  5.            <Columns>  
  6.                 <asp:TemplateField HeaderText="Product Name">  
  7.                     <ItemTemplate>  
  8.                         <asp:LinkButton  
  9.                         ID="LinkProducts"  
  10.                         runat="server"  
  11.                         CommandArgument='<%#Eval("ID")%>'  
  12.                         OnCommand="Product_Command"  
  13.                         Text='<%# Eval("Name")%>'></asp:LinkButton>  
  14.                     </ItemTemplate>  
  15.                 </asp:TemplateField>  
  16.   
  17.                          <asp:BoundField DataField="Description" HeaderText="Description" />  
  18.             </Columns>  
  19.         </asp:GridView>  
  20.     </div>  
  21.     <div id="displayDetails" runat="server">  
  22.     <asp:Label ID="ShowDetails" runat="server"></asp:Label>  
  23.     </div>  
  24.     </form>  
  25. </body> 

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

  1. /// <summary>    
  2. /// Page Load Event    
  3. /// </summary>    
  4. /// <param name="sender"></param>    
  5. /// <param name="e"></param>    
  6. protected void Page_Load(object sender, EventArgs e)    
  7. {    
  8.     List<Product> ProductList = new List<Product>    
  9.     {    
  10.         new Product(){Name="Product1", ID="1",Description = "Description1"},    
  11.         new Product(){Name="Product2", ID="2",Description = "Description2"},    
  12.         new Product(){Name="Product3", ID="3",Description = "Description3"}   
  13.     }  
  14.     ShowProducts.DataSource = ProductList;    
  15.     ShowProducts.DataBind();    
  16. }    
  17. /// <summary>    
  18. /// OnCommand Method for Link Button    
  19. /// </summary>    
  20. /// <param name="sender"></param>    
  21. /// <param name="e"></param>    
  22. protected void Product_Command(Object sender, CommandEventArgs e)    
  23. {    
  24.     //Get the Product ID from the Command Argument    
  25.     String productID = e.CommandArgument.ToString();    
  26.   
  27.     //Call the method to display the selected product ID    
  28.     ShowDetailsOfProduct(productID);    
  29. }    
  30.   
  31. /// <summary>    
  32. /// Show the product id selected    
  33. /// </summary>    
  34. /// <param name="productID"></param>    
  35. protected void ShowDetailsOfProduct(string productID)    
  36. {    
  37.     StringBuilder sbreport = new StringBuilder();    
  38.     sbreport.Append("<table width='95%' border='0' cellpadding='0' cellspacing='0' align='center'>");    
  39.     sbreport.Append("<tr class='tdcolbg'>");    
  40.     sbreport.Append("<td>");    
  41.     sbreport.Append("You have clicked :" + productID);    
  42.     sbreport.Append("</td>");    
  43.     sbreport.Append("</tr>");    
  44.     sbreport.Append("</table>");    
  45.     ShowDetails.Text = sbreport.ToString();    
  46. } 

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.

Server-side-function-in-ASP.NET.jpg

Conclusion

This article discusses 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 postback and also to reduce the performance issues. Since obtaining data from the server and a postback of the entire page is avoided it is always a best practice to go for AJAX.


Similar Articles