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 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.
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="ShowProducts" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:LinkButton
ID="LinkProducts"
runat="server"
CommandArgument='<%#Eval("ID")%>'
OnCommand="Product_Command"
Text='<%# Eval("Name")%>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" />
</Columns>
</asp:GridView>
</div>
<div id="displayDetails" runat="server">
<asp:Label ID="ShowDetails" runat="server"></asp:Label>
</div>
</form>
</body>
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>
{
new Product(){Name="Product1", ID="1",Description = "Description1"},
new Product(){Name="Product2", ID="2",Description = "Description2"},
new Product(){Name="Product3", ID="3",Description = "Description3"}
}
ShowProducts.DataSource = ProductList;
ShowProducts.DataBind();
}
/// <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
ShowDetailsOfProduct(productID);
}
/// <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("<td>");
sbreport.Append("You have clicked :" + productID);
sbreport.Append("</td>");
sbreport.Append("</tr>");
sbreport.Append("</table>");
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.
Conclusion
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.