Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditionally call JavaScript function

I have a "Pay" button where we are starting to accept credit card. I need to call a Javascript function from the server side click event. I tried with Response.Write as below but that does not trigger my function that is defined in the separate .js file. What else can I do?

        protected void btmMakePayment_Click(object sender, EventArgs e)
        {            
            if (user selected credit card)
            {
                Response.Write("<script language='javascript' type='text/javascript'>OpenPayPalDialog();</script>");
            }
            else
            {
                continue with the current server side logic
            }
       }

Thank You in advance.

like image 496
socialMatrix Avatar asked May 10 '26 16:05

socialMatrix


2 Answers

Use ClientScriptManager.RegisterStartupScript, which registers a block of JavaScript to execute when the page loads.

like image 80
System Down Avatar answered May 12 '26 05:05

System Down


You can use ScriptManager.RegisterClientScriptBlock to invoke JS event:

ScriptManager.RegisterClientScriptBlock(this, typeof(this), "JSKey", "JSFunctionName(<param>);", true);
like image 30
Umesh Avatar answered May 12 '26 05:05

Umesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!