views:

29

answers:

2

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.DateTime.Now.ToString();
    ClientScript.RegisterClientScriptBlock(this.GetType(),"success","saveSuccess()");
}
+1  A: 

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.

Nikos Steiakakis
thanks Nikos,I am using this approach because I want to call the script only after a certain event has occured server side (Like some records saved in the database)
Popo
You mean you want the 'saveSuccess()' script to be called, only if the records are saved? Will the button (button4) need to call the function that will save the records? If so, then the button should probably not have the submit behaviour to false. Am I getting this right?
Nikos Steiakakis
Hi, Button4 will save the records server side. On success, it would call the client script which would display a status message via a function. Isn't there any way round this ?
Popo
Well, you have to submit the request with button4, so submit behaviour should be set to true. When the "save" is finished, then you can use ClientScript.RegisterClientScriptBlock(this.GetType(),"success","saveSuccess()"); If you use and UpdatePanel then you should use the ScriptManager to register the script.
Nikos Steiakakis
Also, if the "save" takes long, and you want to continue working while the application saves the records, you should consider using a BackgroundWorker, however I am not sure if this is the case.
Nikos Steiakakis
I have my button outside the update panel, requirements are such that I cannot include the button inside the update panel nor change the submit mode to true.
Popo
Popo
If my understanding of the situation is correct, then you should probably call ScriptManager.RegisterClientScriptBlock(typeof(Page), "scriptname", "saveSuccess();", true) when the Asynch request is completed, and you re-enable the button? Would that work for you?
Nikos Steiakakis
Thanks Nikos, you are correct. Problem was solved by using scriptManager.StartupScript. Thumbs up =)
Popo
Glad I've helped! I'll edit my response to assist any other users with the same problem.
Nikos Steiakakis
A: 
ClientScript.RegisterStartupScript(this.GetType(), "success", "saveSuccess()", true);
y34h