Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5933033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:55:02+00:00 2026-05-22T14:55:02+00:00

ScriptManager.RegisterStartupScript(page, page.GetType(), KEY, updateDockTitle(‘ + ClientID + ‘, ‘ + string.Format(format, ChartName, No Data

  • 0
ScriptManager.RegisterStartupScript(page, 
    page.GetType(),
    "KEY",
    "updateDockTitle('" + ClientID + "', '" + string.Format(format, ChartName, "No Data To Display") + "');".Replace("-", "\\-"),
    true);

ChartName has the ability to contain special characters (e.g. Someone’s Chart). If allowed as is this causes an error the code hits the ' when it was not expecting it.

What’s the proper way to handle this scenario? Do I need to know all characters that could cause issues and replace them with escaped out versions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:55:02+00:00Added an answer on May 22, 2026 at 2:55 pm

    I’ve found this comprehensive solution handy:

    private static readonly Regex scriptTagRegex = new Regex(
        "script", RegexOptions.IgnoreCase | RegexOptions.Multiline);
    
    /// <summary>
    ///     Processes the provided string, creating a quoted JavaScript string literal.
    /// </summary>
    /// <param name="str">The string to process</param>
    /// <returns>A string containing a quoted JavaScript string literal</returns>
    public static string JavaScriptStringLiteral(string str)
    {
        var sb = new StringBuilder();
        sb.Append("\"");
        foreach (char c in str)
        {
            switch (c)
            {
                case '\"':
                    sb.Append("\\\"");
                    break;
                case '\\':
                    sb.Append("\\\\");
                    break;
                case '\b':
                    sb.Append("\\b");
                    break;
                case '\f':
                    sb.Append("\\f");
                    break;
                case '\n':
                    sb.Append("\\n");
                    break;
                case '\r':
                    sb.Append("\\r");
                    break;
                case '\t':
                    sb.Append("\\t");
                    break;
                default:
                    int i = (int)c;
                    if (i < 32 || i > 127)
                    {
                        sb.AppendFormat("\\u{0:X04}", i);
                    }
                    else
                    {
                        sb.Append(c);
                    }
                    break;
            }
        }
        sb.Append("\"");
    
        // If a Javascript tag contains "</script>", then it terminates a
        // script block.  Start by replacing each 's'/'S' with an escape
        // sequence so it doesn't trigger this.
        return scriptTagRegex.Replace(
            sb.ToString(),
            m => (m.Value[0] == 's' ? "\\u0073" : "\\u0053") + m.Value.Substring(1));
    }
    

    It should replace all invalid JS characters and also make sure there’s no breakage if your script is inline (which it would be in your case). Note that it already generates the quotes for the string literal, so keep that in mind if you decide to use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ScriptManager.RegisterStartupScript(this, this.GetType(), redirectScript, window.location.href='Thankyou.aspx?'Service= + hidService.Value + ;,true); Getting error: Expected ; Please help
I'm trying to call ScriptManager.RegisterStartupScript from page A so that when page B loads
I have this code: ScriptManager.RegisterStartupScript(this, GetType(), alert, alert('Ouve algum problema nos itens + text
I have a simple page with my ScriptManager and my UpdatePanel , and my
How to include loader image without usng AJAX controls like Scriptmanager, UpdateProgerss. Note: Page
What is equivalent of ScriptManager.RegisterStartUpScript() of Asp.Net in Php? I want to call some
Why can I not add ScriptManager control or UpdatePanel to a page in Visual
I am using ScriptManager.RegisterStartupScript to register calls to a large number of JS functions.
I want use ajax.net to do some js. like below: ScriptManager.RegisterStartupScript(Submit, typeof(Button), alert, location.href='test.aspx';,
I use ScriptManager.RegisterStartupScrit from Page_Load and it works perfect. When I move the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.