Click here to Skip to main content
15,917,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to concatenate messages to a message while using ClientScript.RegisterStartupScript in asp.net with c#. While concatenating, no message is coming while the same works on appending the same.

What I have tried:

MY Code is as below:
C#
// concatenating a message in <pre>ClientScript.RegisterStartupScript
string strTest += "\r\nInvalid OIC Type for Loans!";
ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Request saved successfully and send to Maker for verification" + "(Test - " + strTest + "); window.location.href = 'Test_Initiation.aspx" + queryString + "'; ", true);

// Without concatenating a message in ClientScript.RegisterStartupScript
ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Request saved successfully and send to Maker for verification'); window.location.href = 'DDS300_Initiation.aspx" + queryString + "'; ", true);
Posted
Updated 9-Mar-20 10:37am
v3

There seems to be a syntax error in your alert call, plus an incorrect usage of double quotes in place of a single quote. Try
JavaScript
ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Request saved successfully and send to Maker for verification" + "(Test - " + strTest + ")'); window.location.href='/KB/answers/Test_Initiation.aspx" + queryString + "'; ", true);
instead.
 
Share this answer
 
Comments
Maciej Los 10-Mar-20 2:56am    
5ed!
As well as the missing single quote, your message needs to be properly encoded. You are currently outputting:
JavaScript
alert('Request saved successfully and send to Maker for verification(Test - 
Invalid OIC Type for Loans!); window.location.href = 'Test_Initiation.aspx?...';
which won't work, because normal JavaScript strings can't contain unescaped line-breaks.

You should be outputting:
JavaScript
alert('Request saved successfully and send to Maker for verification(Test - \r\nInvalid OIC Type for Loans!'); window.location.href = 'Test_Initiation.aspx?...';
Use HttpUtility.JavaScriptStringEncode to encode your message before appending it. You should probably encode the queryString as well, just to be safe.
C#
ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Request saved successfully and send to Maker for verification" + "(Test - " + HttpUtility.JavaScriptStringEncode(strTest) + "'); window.location.href = 'Test_Initiation.aspx" + HttpUtility.JavaScriptStringEncode(queryString) + "'; ", true);
I think it would probably be cleaner to use string.Format, to build the script:
C#
ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", string.Format("alert('Request saved successfully and send to Maker for verification(Test - {0}'); window.location.href = 'Test_Initiation.aspx{1}';", HttpUtility.JavaScriptStringEncode(strTest), HttpUtility.JavaScriptStringEncode(queryString)), true);
That should make it easier to spot any syntax errors in your script.

HttpUtility.JavaScriptStringEncode Method (System.Web) | Microsoft Docs[^]
 
Share this answer
 
Comments
Maciej Los 10-Mar-20 2:57am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900