3

Possible Duplicate:
How to call javascript function from c#

Is there a way to call a javascript function from a button click in code behind in C#? So basically I have a registration form and when you click the button, then all the data is processed in the code behind and the form is submitted. But I need to execute some javascript and I need it to be executed on the button click and in code behind.

so something like:

    protected void btnDoRegister_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
         C# code is here doing form processing
         // Now I need to Execute javascript here.
    }

Is this possible and how would I go about doing it, if it is?

Thanks!

2
  • You can't execute JavaScript in server side. Commented Jun 28, 2012 at 14:37
  • 1
    Is there a specific reason you want to execute the Javascript before the server-side code? If you were doing validation, you could have the button execute the JS before it is handed off to the server. Example: <input type="submit" onclick="return validateForm();" /> which will only pass to the server if validateForm returns true. Commented Jun 28, 2012 at 14:40

4 Answers 4

4
protected void btnDoRegister_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
     C# code is here doing form processing
     // Now I need to Execute javascript here.
     string script = "<script type=\"text/javascript\"> functionToExecute(); </script>";
     ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", script);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Executing javascript on server is not possible as javascript executes on client(browser). You can execute javascript after postback but it is seldom required. Normally Javascript executes on client side and executes before the request is received at server and executed on server. This way you have two event binded with btnDoRegister one executes on client and ohter executes on server.

On Page_Load bind javascript event with button.

In Code Behind

btnDoRegister.Attributes.Add("onclick", "funForButton();");

In aspx HTML

<script language="javascript" type="text/javascript">     
function funForButton()
{
   alert("About to send request to server");
}

</script>

The Server event you already have

protected void btnDoRegister_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
     C# code is here doing form processing
     // Now I need to Execute javascript here.
}

Comments

0

There's two ways I see off the top of my head:

The first is do some AJAX polling on the javascript side. It can poll for changes it needs to make via a rest api or something, and your c# code can update that list of changes.

The second is to use a javascript socket io implementation and maintain a persistent connection.

I'd highly recommend the first, but the second is an option depending on your environment.

Comments

0

If i understand correctly, you want to click the button, have some javascript do its thing, then return to the serverside, do the C# code and finish it off with executing some javascript (on the client again).

Let's assume i got it right then you would have to do 3 things.

First, add the OnClientClick="return yourfunction();" to the button in your aspx page. Depending on if you want the serverside code to be executed, make sure the yourfunction() in javascript returns false or true.

Then code your c# code in the btnDoRegister_Click. And at the end of this, add a javascript call to a session variable and use this in your aspx code.

example: c#

String newScript = ""

If (Session("yourscript") = Nothing) { Session("yourscript") = "" }

newScript = String.Format("<script type=text/javascript>")

newScript = String.Format("{0}yoursecondscript()", newScript)

newScript = String.Format("{0}</script>", newScript)

Session("yourscript") = newScript

and then in your aspx page add somewhere in your page:

<%=Session("yourscript")%>

Keep in mind where to place it if you are using Ajax because not the entire page will be refreshed. In that case, place it inside the updateframe.

Now the sequence is: - click -> yourfunction() -> C# code -> yoursecondscript

hope this helps....

Comments