How to add onclientclick attribute to a Linkbutton from C#code?

5.4k Views Asked by At

I am trying to add onclientclick attribute to a linkbutton that has been created in the C# code only which gives error. Following is the code for the same :

LinkButton imghelp = new LinkButton();
imghelp.ID = "btnhelp";
imghelp.Text = "<i class = 'fa fa-question-circle-o'></i>";
imghelp.onClientClick = "javascript:ShowHelp(\"" + Languagecode + "\",\"" + context + "\",\"" + subcontext + "\")";

The error is : "System.Web.UI.WebControls.LinkButton does not contain a definition for onClientClick"

2

There are 2 best solutions below

1
Nitin Kumar On

you should have to use imghelp.Attributes.Add("onClientClick", "Your function here"); because if you directly add event like you have done in your code it will always throws an error.

0
LateshtClick.com On
imghelp.Attributes.Add("onclick", "return ShowHelp(your parameter)");

your javascript function

 function ShowHelp(your parameter)
 {
       // your code
 }