0

I am having trouble accessing a Javascript function from my code behind file. I need to do this as I am using the GoogleMaps JS API to add markers to a map based on addresses retrieved from my database. I have a function called AddMarker that takes in the address as a parameter, so I need to be able to call that from my code behind file in the page_load function.

To simplify the question, how I can I call this javascript function to display an alert with a string passed from my code behind file?:

function hello(message)
{
alert(message)
}

Thanks in advance!

P.S Either vb or c# will do :)

4 Answers 4

2

That should do:

 protected void Page_Load(object sender, EventArgs e) 
 { 
     string bing = "link";
     Response.Write(@"<script language='javascript'>alert(bing);</script>");
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Bad idea - this will appear on top of everything in the HTML output and might ruin the whole design.
1

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</p>

Then in your javascript:

function hello()
{
    var m = document.getElementById('message').innerHTML;
    alert(m);
}

Comments

1

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

<script type="text/javascript">

$(document).ready(function(){

hello("myAspString");

});

</script>

Replace myAspString with your content, making sure to preserve the quotes as needed by JS.

1 Comment

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.
1

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 <script> 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 <script> 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.

5 Comments

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: <script type="text/javascript"> function hello(message) { alert(message) } </script> Any ideas where I am going wrong?
The above comment is missing a semi-colon after alert(message), I have added it in but it doesn't make a difference.
If you replace String.Format("hello('{0}');", value.Replace("'", "\\'")) to be just "alert('test 1 2 3');" do you see alert?
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?
Thanks for your help pal. Not sure why I couldn't get your method to work but I appreciate the help. I managed to get around this by writing the parameters to hidden labels then referencing the labels from the JS.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.