Call JS function from CodeBehind file?

You may consider this a hack but you could always put your message in an html element like: p id='message' style='display: none;'>Your Message.

You may consider this a hack but you could always put your message in an html element like: Your Message Then in your javascript: function hello() { var m = document. GetElementById('message'). InnerHTML; alert(m); }.

That should do: protected void Page_Load(object sender, EventArgs e) { string bing = "link"; Response. Write(@""); }.

Bad idea - this will appear on top of everything in the HTML output and might ruin the whole design. – Shadow Wizard Oct 24 at 10:31.

I'm not certain if this is best practice, but you could just render out a call to the JS function somewhere in your page, after it has been defined. Or you could use jQuery to delay the call until everything in the page is rendered. So Replace myAspString with your content, making sure to preserve the quotes as needed by JS.

Thanks for the reply. But the problem is that the parameters will be coming from the database and therefore the function needs to be called from within the code behind file. – Leopold Scotch Oct 24 at 10:59.

This code will cause the function hello to be called with value from the code behind: string value = "world"; Page.ClientScript. RegisterClientScriptBlock(this.Page.GetType(), "my_script", string. Format("hello('{0}');", value.

Replace("'", "\\'")), true); The RegisterClientScriptBlock will append proper tags to the HTML output sent to browser and inject your code in there. The second argument is the "key" of the script, it enable you to have several statements and check if you already registered specific statement based on the key. The last argument tells the framework to add tags for you.

You need to replace any single quotes with the proper escape sequence to avoid breaking the string when it contains single quotes as this is the "delimeter" used to pass the value to the function.

Cheers for the reply. I have followed your method but the alert still doesn't show. My page load sub: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.

EventArgs) Handles Me. Load Dim value As String = "world" age.ClientScript. RegisterClientScriptBlock(Me.Page.GetType(), "my_script", String.

Format("hello('{0}');", value. Replace("'", "\\'")), True) End Sub And my JS: Any ideas where I am going wrong? – Leopold Scotch Oct 24 at 10:49 The above comment is missing a semi-colon after alert(message), I have added it in but it doesn't make a difference.

– Leopold Scotch Oct 24 at 10:56 If you replace String. Format("hello('{0}');", value. Replace("'", "\\'")) to be just "alert('test 1 2 3');" do you see alert?

– Shadow Wizard Oct 24 at 11:05 No unfortunately :( – Leopold Scotch Oct 24 at 11:13 Sorry can't test it as I'm working only with C# and it's working just fine using your exact function. Check the HTML source of the page - you see the alert('test 1 2 3') you wrote? – Shadow Wizard Oct 24 at 12:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions