1

I am working with VS, a web form application, and I want to generate in the code-behind (C#) a JavaScript function defined in a JavaScript file in the project,. I have tried different ways ,such as this line of code:

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "Function_Name", true);

However, it can't resolve the name of my function since it's "not defined" as it's shown as a JavaScript error. But it works fine with a simple line of JavaScript code put in the Function_Name field (like alert("something")).

Any help with this please?

4
  • 1
    Function_Name !== function_Name - different capitalisation. Commented Aug 15, 2019 at 7:41
  • Function_Name ,function_Name , that's just a label . the problem is that the name of my defined function is not known in c# code . I edited the question . Commented Aug 15, 2019 at 7:45
  • 1
    If you use two different names for the function, and different capitalisation counts as different, then you would get "not defined". Again, the two are not the same. If that's not the case for you, then your question doesn't make it clear. Commented Aug 15, 2019 at 7:46
  • @VLAZ i know that's undeniable , when i call my function , it shows it's not defined , with it's same name for sure , (i edited the question ) Commented Aug 15, 2019 at 7:53

1 Answer 1

1

C#

define your javascript inside the C# code as text

Type type = this.GetType();
String key = "CallMyFunction";
ClientScriptManager cs = Page.ClientScript;

if (!cs.IsClientScriptBlockRegistered(type, key))
{
  StringBuilder script = new StringBuilder();
  script.AppendLine("<script type=\"text/javascript\">");
  script.AppendLine("  function Function_Name() {");
  script.AppendLine("    frmMain.Message.value = 'Hello World';");
  script.AppendLine("  }");
  script.AppendLine("</script>");

  cs.RegisterClientScriptBlock(type, key, script.ToString(), false);
}

or read your javascript from a .js file

<script type="text/javascript">
  function Function_Name() {
    frmMain.Message.value = 'Hello World';
  }
</script>
Type type = this.GetType();
String key = "CallMyFunction";
ClientScriptManager cs = Page.ClientScript;

if (!cs.IsClientScriptBlockRegistered(type, key) && File.Exists(path))
{
  string script = File.ReadAllText(path);

  cs.RegisterClientScriptBlock(type, key, script, false);
}

HTML - Body

<body>
  <form id="frmMain" runat="server">
    <input type="text" id="Message" />
    <input type="button" value="Click!" onclick="Function_Name()" />
  </form>
</body>

If you need a one-liner:

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "function Function_Name() { frmMain.Message.value='Hello World'; }", true);

or

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "<script type=\"text/javascript\">function Function_Name() { frmMain.Message.value='Hello World'; }</script>", false);

EDIT:

Using includes

String includeKey = "MyInclude";
String includeFile = "/myInclude.js";
String scriptKey = "CallMyFunction";
String script = "Function_Name();"; //Function inside your included js file.
Type type = GetType();
ClientScriptManager cs = Page.ClientScript;

//register the js file containing the function
if (!cs.IsClientScriptIncludeRegistered(includeKey))
{
    cs.RegisterClientScriptInclude(includeKey, includeFile);
}

//register the script to call the function 
if (!cs.IsClientScriptBlockRegistered(scriptKey))
{
    cs.RegisterClientScriptBlock(type, scriptKey, script, true);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ! that works fine , but what can i do when there are imports in the JavaScript code ? , because that generates errors in this situation .
thanks , that can be resolved with a single line in html file by adding type="module" <script type="module" src="./src/index.js"></script>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.