ASP.NET Workaround: Calling a JavaScript Function from a HyperLinkField in a GridView

ASP.NET Workaround: Calling a JavaScript Function from a HyperLinkField in a GridView

If you place a HyperLinkField in a GridView in an ASP.NET page and link it to a JavaScript function, you’ll find that it doesn’t work properly. The problem is that the URL specified by the HyperLinkField’s NavigateUrl fails to render when you use a JavaScript reference (such as "Javascript:func();") rather than a standard URL.

One possible workaround is to create an item template column containing a hyperlink.

As a workaround, however, you can use the RowDataBoundEvent to prepare a proper call to the JavaScript function. To make this work, developers need to type "Javascript." (with a period) instead of "Javascript:" (with a colon) in the designer.

Here’s an example:

void grid_RowDataBound(object sender, GridViewRowEventArgs e){   //if it is not DataRow return.   if (e.Row.RowType != DataControlRowType.DataRow)   {      return;   }   //Loop thru the cells changing the "." to ":" in hyperlink navigate URLs   for (int i = 0; i < e.Row.Cells.Count; i++)   {      TableCell td = e.Row.Cells[i];      if (td.Controls.Count > 0 && td.Controls[0] is HyperLink)      {         HyperLink hyperLink = td.Controls[0] as HyperLink;         string navigateUrl = hyperLink.NavigateUrl.ToLower();         hyperLink.NavigateUrl = hyperLink.NavigateUrl.Replace(            hyperLink.NavigateUrl.Substring(            navigateUrl.IndexOf("javascript."), "javascript.".Length),             "javascript:");      }   }}
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes