-1

I am developing an IE Addon using C#. I have javascript functions in a js file. I need to attach the js file to C# and call js functions from C#, send value to js and it must return a value.

JS File - sample.js :

function sample (str) {
   //js code
}

C# File :

private void button1_Click(object sender, EventArgs e){
   //Need to call sample(str) and pass 'str' value to the js function.
}

P.S : I tried the below code but getting object reference not set to instance of an object

Page page = HttpContext.Current.CurrentHandler as Page;   
page.ClientScript.RegisterClientScriptInclude("sample","sample.js");
page.ClientScript.RegisterStartupScript(typeof(Page),"Test","<script type='text/javascript'>sample('str');</script>");
abby
  • 1
  • 3
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mybirthname Nov 21 '16 at 10:38
  • Thanks. But can you suggest a working code for the above case? What is wrong in the code? – abby Nov 21 '16 at 12:08

1 Answers1

0

Currently, your ASP.Net application must be calling your C# callback:

<asp:Button ID="button1" runat="server" Text="Do something" onclick="button1_Click" />

One way to solve this problem is to simply add an OnClientClick attribute:

<asp:Button ID="button1" runat="server" Text="Do something" onclick="button1_Click" OnClientClick="sample('someValue');" />

It all depends where this "str" parameter value comes from, which you want to pass to the JavaScript function...

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
  • I am using only c# and not asp.net. Am creating a browser extension using addin express. Could you provide a code in c# when i could send a value to js and it will return another value – abby Nov 22 '16 at 06:58