1

I have Client-Side button event. After click call sgnPdf(ID) if I can sign PDF.

[MyForm.aspx]

 <script type="text/javascript">
    <% if (ViewData.CanSignPdf){ %>
    $(function() {
        $("#<%=btnSend.ClientID %>").click(function() {
            if ($('.IgnoreCheckBox').is(':checked')) {
                sgnPdf(<%=ViewData.NoticeID %>);
                return false;
            };
        });
    });
    <% } %>
</script>

But I need call this function from Server side after button click.

[MyForm.aspx.cs]

  protected void BtnSendClick(object sender, EventArgs e)
    {
        FormUnbindData();
        SaveMYData();
        //Here i want call client function sgnPdf(ID). How can I do this?
        ShowMyMessage();
    }

OK, you need to do just simply. For example call alert("message") in there.


Ok, its works!!! but i need call client-side method(javascript) from server-side(C#) synchronously, becouse client-side method not starting until server-side method ends (for example button_click event).


I try this:

            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "anyId", "<script type='text/javascript'>alert('This is my alert message');</script>", true);

and no action, nothing.

1
  • 1
    +1 for the nice question by a newbie!
    – Viral Shah
    Sep 25, 2012 at 13:31

3 Answers 3

1

Use this when button click:

 ClientScript.RegisterClientScriptBlock(Page, typeof(Page),sgnPdf(ID), true); 

OR
    <asp:Button OnClientClick = "sgnPdf(ID)"/>

Use this on page load:

RegisterStartupScript("Unique key", "sgnPdf(ID);"); 

Here is a Reference

3
  • I think RegisterStartupScript works only in Page_Load(Object sender, EventArgs e) event.
    – Emis
    Sep 25, 2012 at 13:45
  • But i need call sgnPdf(ID) when after button click FormUnbindData() and SaveMYData(). THEN call sgnPdf(ID).
    – Emis
    Sep 25, 2012 at 13:59
  • @Emis-- you can use RegisterClientScriptBlock for your need. Sep 26, 2012 at 5:53
1

try following....

protected void BtnSendClick(object sender, EventArgs e)
     {
         FormUnbindData();
         SaveMYData();
         **Edit**
         ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "anyId", "<script language='javascript'>sgnPdf('"+ID+"');</script>", true);            

         ShowMyMessage();
     } 
1
  • if you were not able to call the javascript function from first method,i have changed it now,just an overload,try to use it and check whether it solves you problem... Sep 26, 2012 at 11:34
0

If you are using UpdatePanel on your page then you can call client side function from server side using ScriptManager.RegisterStartupScript

Use below code in button click event

ScriptManager.RegisterStartupScript(this, this.GetType(), "script234", "sgnPdf('"+ID+"');", true);

If you are not using UpdatePanel on your page then you can call client side function from server side using ClientScript.RegisterStartupScript

use below code in button click event

ClientScript.RegisterStartupScript(this.GetType(), "script234", "sgnPdf('"+ID+"');", true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.