1

I have the following code which should call a function called ShowPopup in my client side script but for reason, when I call this function nothing happens.

  string pg = "Test";
  ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup(pg);", true);

If I do the following:

ClientScript.RegisterStartupScript(
                   this.GetType(), "Popup", "ShowPopup('Test');", true);

it works fine. It does show up in the popup. Any idea what I may be doing wrong.

1
  • show your popup js function Commented Sep 18, 2014 at 18:28

3 Answers 3

10

The problem is ShowPopup is expecting a string value.

Correct Code

string pg = "Test";
ClientScript.RegisterStartupScript(this.GetType(), "Popup",
   string.Format("ShowPopup('{0}');", pg), true);

About C# code will generate the following valid javascript -

<script>
   ShowPopup('Test');
</script>

Error Code

ClientScript.RegisterStartupScript(this.GetType(), "Popup", 
   "ShowPopup(pg);", true);

Notice that above code C# will generate the following invalid Javascript -

<script>
   ShowPopup(pg); // Invalid Javascript code
</script>
Sign up to request clarification or add additional context in comments.

Comments

3

If you Used Update Panels Then You can Use:

string pg = "Test";
    ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "alert('"+pg+"');", true);

Other Wise You can Use

string pg = "Test";
     ClientScript.RegisterStartupScript
                (GetType(),Guid.NewGuid().ToString(), "alert('"+pg+"');",true);

in your case

string pg = "Test";
 ClientScript.RegisterStartupScript
            (GetType(),Guid.NewGuid().ToString(), "ShowPopup('"+pg+"');",true);

Comments

0

You can also use String interpolation:

string pg = "Test";
ClientScript.RegisterStartupScript(this.GetType(), "Popup", $"ShowPopup('{pg}');", true);

Comments

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.