1

I added a javascript function in a page

 <head>
    <script type=text/javascript>
        function show_Alert(error)
        {
           alert(error);
         }
    </script>
</head>

and on button click I am doing this

Protected void btn_Click(object o,Eventargs e)
{
    StringBuilder str = new StringBuilder();
    str.AppendLine("show_Alert('XYZ error')");
    ClientScript.RegisterStartupScript(GetType(),"Alert",str.ToString(),true);
}

But it throws JS error show_Alert is not defined :(

Any Idea, what is wrong here??

Thanx

1
  • Sorry That was not a issue, I am editing question Commented Dec 23, 2010 at 14:07

3 Answers 3

4

Your script tag is wrong.
Change it to

<script type="text/javascript">

However, I don't think that's the issue.
I suspect that RegisterStartupScript is emitting its <script> block before the one with your function, so that it ends up calling the function before it exists.
Check where each <script> is in the rendered source.

Sign up to request clarification or add additional context in comments.

2 Comments

SLaks RegisterStartupScript creates new <script> block and not in a head. I think that is a problem.
If it's after the <head>, it shouldn't be a problem. Put a normal alert() call at the beginning of each script and check what order they're alerted
2

Make sure your <script> element is valid, like this:

<head>
  <script type="text/javascript">
    function show_Alert(error)
    {
      alert(error);
    }
  </script>
</head>

If it's not well formed, or the type is unrecognized (your case has both) then the script inside will be ignored, since the browser doesn't know how to handle it.

Comments

0

Here's an example:

<%@ Page Language="C#" %>
<script type="text/C#" runat="server">
    protected void BtnClick(object sender, EventArgs e)
    {
        var str = "XYZ Error";
        var script = new StringBuilder();
        script.AppendFormat("showAlert('{0}');",
            HttpUtility.JavaScriptStringEncode(str)
        );
        ClientScript.RegisterStartupScript(GetType(), "alert", script.ToString(), true);
    }
</script>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        function showAlert(error) {
            alert(error);
        }    
    </script>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:LinkButton ID="Btn" runat="server" Text="Click me" OnClick="BtnClick" />
    </form>
</body>
</html>

A very important thing to pay attention to is to properly encode the string you are passing to the showAlert function. Notice that it is encoded:

script.AppendFormat("showAlert('{0}');",
    HttpUtility.JavaScriptStringEncode(str)
);

If you don't encode it and the string contains some special characters like ' for instance, your script will break.

2 Comments

HttpUtility doesn't have any option of JavaScriptStringEncode :(
@BreakHead, yeah that's .NET 4.0. You could use new JavaScriptSerializer().Serialize(str) as an equivalent in older versions of .NET.

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.