简体   繁体   中英

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. I am getting an error as the function is not defined.

Javascript:

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

    }

C# Code Behind:

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.

Remove the code written in PostBk() method and replace it with below code :

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

add following code in page load

Ajax.Utility.RegisterTypeForAjax(typeof(FrmTableReservation))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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