简体   繁体   English

无法从后面的C#代码中调用javascript函数

[英]Unable to call javascript function from c# code behind

I am trying to call a javascript function from C# while post back below is my code. 我试图调用从C#中的javascript函数而回后下面是我的代码。 I am getting an error as the function is not defined. 由于未定义函数,因此出现错误。

Javascript: Javascript:

    function postBackResponse(){ 
       alert("Success"); 

    }

C# Code Behind: 后面的C#代码:

protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
  PostBk();
}

}
public void PostBk(){

string displayTasks = "PostPer";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "TasksListString = '" + displayTasks + "'; postBackResponse();", true);

}

Replace following line 替换以下行

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "TasksListString = '" + displayTasks + "'; Page_Load();", true);

with

this.ClientScript.RegisterStartupScript(this.GetType(), "key", "postBackResponse();", true);

the function is not defined. 该函数未定义。

As suggested in error, the function you suppose to call must be defined in your javascript. 如错误提示所示,您必须调用的函数必须在javascript中定义。

Remove the code written in PostBk() method and replace it with below code : 删除用PostBk()方法编写的代码,并将其替换为以下代码:

public void PostBk()
{
    ScriptManager.RegisterStartupScript(this, typeof(Page), "script", "postBackResponse()", true);
}

add following code in page load 在页面加载中添加以下代码

Ajax.Utility.RegisterTypeForAjax(typeof(FrmTableReservation)) Ajax.Utility.RegisterTypeForAjax(typeof(FrmTableReservation))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM