c#代码中未定义JavaScript函数

c#代码中未定义JavaScript函数,javascript,c#,Javascript,C#,我正在使用VS,一个web表单应用程序,我想在代码隐藏(C#)中生成一个JavaScript函数,该函数在项目的JavaScript文件中定义,。 我尝试过不同的方法,例如这行代码: Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "Function_Name", true); 但是,它无法解析我的函数名,因为它是“未定义”的,因为它显示为JavaScript错误。但是在Function\u N

我正在使用VS,一个web表单应用程序,我想在代码隐藏(C#)中生成一个JavaScript函数,该函数在项目的JavaScript文件中定义,。 我尝试过不同的方法,例如这行代码:

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "Function_Name", true);
但是,它无法解析我的函数名,因为它是“未定义”的,因为它显示为JavaScript错误。但是在
Function\u Name
字段中放一行简单的JavaScript代码(比如
alert(“something”)
)就可以了

有什么帮助吗?

C#

将C代码中的javascript定义为文本

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);
}
HTML-正文

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


功能名称
!==<代码>函数名称-不同的大写字母。函数名称,函数名称,这只是一个标签。问题是我定义的函数的名称在c#代码中是未知的。我编辑了这个问题。如果你对函数使用了两个不同的名称,并且不同的大写表示不同,那么你会得到“未定义”。再说一次,两者并不相同。如果你的情况不是这样,那么你的问题就不清楚了。@VLAZ我知道这是不可否认的,当我调用我的函数时,它表明它没有定义,肯定是同名的,(我编辑了这个问题)谢谢!这很好,但是当JavaScript代码中存在导入时,我能做什么,因为在这种情况下会产生错误。谢谢,这可以通过在html文件中添加type=“module”一行来解决
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);
}
<body>
  <form id="frmMain" runat="server">
    <input type="text" id="Message" />
    <input type="button" value="Click!" onclick="Function_Name()" />
  </form>
</body>
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "function Function_Name() { frmMain.Message.value='Hello World'; }", true);
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "<script type=\"text/javascript\">function Function_Name() { frmMain.Message.value='Hello World'; }</script>", false);
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);
}