Call client script from button with submit behaviour false?
How do I call a client script function from a server side button click(with submit behaviour set to false) .
protected void Button4_Click(object sender, EventArgs e)
{
//System.Threading.Thread.Sleep(2000);
lbDateTime.Text=System开发者_如何学Go.DateTime.Now.ToString();
ClientScript.RegisterClientScriptBlock(this.GetType(),"success","saveSuccess()");
}
You should set the OnClientClick attribute of the Button like
OnClientClick="saveSuccess(); return false;"
this way you keep the sumbit behaviour to false, and you also call the client script function.
Also, if you want to do this via server side code you can add this to your Page_Load
Button4.Attributes["OnClientClick"] = "saveSuccess(); return false;";
However, if you want to call the script after the "save" has completed, and you are using asynchronous jobs, and an UpdatePanel, then you should call
ScriptManager.RegisterClientScriptBlock(typeof(Page), "savescript", "saveSuccess();", true);
after the asynch job has finished, and the data have been saved.
ClientScript.RegisterStartupScript(this.GetType(), "success", "saveSuccess()", true);
精彩评论