Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My JS function is not fired. The 2 pieces of code in my app are below:
C#
string strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "ShowSubscribe(" + strUrl +")", true); 

C#
function ShowSubscribe(url) {
    var ans = confirm("Do you want to subscribe ?");
    if (ans == true) { //Redirect to other page for subscription.
        window.location = url + "ResetPassword.aspx";
    } else {
        //alert("You have click the cancel button.");
    }
}

I know something is wrong on this line of code:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "ShowSubscribe(" + strUrl +")", true);

How can it be corrected? Thanks.
Posted
Comments
Wombaticus 20-Mar-15 19:29pm    
I would think the strUrl in the 4th parameter needs to be eclosed in apostophes thus:
"ShowSubscribe('" + strUrl + "')"

1 solution

The answer to this question has been given inside the comments section, but I would like to add a few more things to that.

What problem was, that your JavaScript was expecting a string-like data to work on, but you were passing a token; variable-like. Which would have been logged as undefined in your console if you would have a look at it.

The solution to it, is to use '' inside your double quotes; to convert the concatenated data as a string.

C#
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), 
              "msg", "ShowSubscribe('" + strUrl +"')", true);
              // Would yield ShowSubscribe("value of strUrl")


There is another tip[^] that I have posted for similar conditions, where you miss these concatenations and other such minor mistakes and keep scratching your head to notice where you missed it. String concatenations always cause problems, either pass parameters to String.Format()[^], or you should use StringBuilder[^] where you are going to use a lot of concatenations.
 
Share this answer
 
Comments
s yu 23-Mar-15 7:38am    
Wonderful! Thanks for your solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900