Trying to display error message in check list format but not working

i am doing some validations before inserting the record into the database, i have to display a checklist format error so i have the code below.

This Code Works

 string script = "alert(\"We don't have standards entered for this item on this line.\");";
            ScriptManager.RegisterStartupScript(this, GetType(),
                          "ServerControlScript", script, true);

Open in new window


I am trying to achieve this:

 if (useful / runtime > 1)
                {
                    List<string> message = new List<string>();
                    message.Add("You have a TE>100%.");
                    message.Add(" Please check your values:");
                    message.Add("- Volume produced (quantity and unit)");
                    message.Add("- Start and Stop times");
                    message.Add(" - Setup and Break");
                    message.Add(" - Item Code(s) and Line/Machine");
                    string script = "alert(" + message + "\");";
                    ScriptManager.RegisterStartupScript(this, GetType(),
                                          "ServerControlScript", script, true);
}

Open in new window

Capture33444.PNG
Aravind RanganathanWindows Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Obadiah ChristopherCommented:
It’s probably because of the >100% line. Coz it’s probably thinking that you are trying to inject malicious script.

Try to HtmlEncode all the string values

Check msdn for Server.HtmlEncode
Aravind RanganathanWindows Application DeveloperAuthor Commented:
@Obadiah Christopher, the >100% works on the script when i add just that, i am looking to see how can i add multiple string values to display like

   
  • some rule
   
  • another rule
   
  • another rule
i want in that format on the same box.
Miguel OzSoftware EngineerCommented:
YOu need to convert your list into string, replace:
string script = "alert(" + message + "\");";

Open in new window

with
string script = "alert('" + string.Join("\n", message .ToArray()) + "');";

Open in new window


for more info check:
https://stackoverflow.com/questions/1841452/new-line-in-javascript-alert-box
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Aravind RanganathanWindows Application DeveloperAuthor Commented:
@Miguel Oz your answer did not work for me do you have any other suggestions??
Miguel OzSoftware EngineerCommented:
What exactly did not work? any error messages/formatting?
I just noticed that there is an extra blank space in my original code:
string script = "alert('" + string.Join("\n", message.ToArray()) + "');";

Open in new window

This code is just converting the list to a string join by a new line as per example in the link below:
https://stackoverflow.com/questions/1841452/new-line-in-javascript-alert-box
Aravind RanganathanWindows Application DeveloperAuthor Commented:
@ Miguel Oz this is the error i am getting:
Captures.PNG
         List<string> message = new List<string>();
            message.Add("You have a TE>100%.");
            message.Add(" Please check your values:");
            message.Add("- Volume produced (quantity and unit)");
            message.Add("- Start and Stop times");
            message.Add(" - Setup and Break");
            message.Add(" - Item Code(s) and Line/Machine");
            //string script = "alert('" + string.Join("\n", message.ToArray()) + "');";
            string script = "alert('" + string.Join("\n", message.ToArray()) + "');";
            ScriptManager.RegisterStartupScript(this, GetType(),
                                  "ServerControlScript", script, true);

Open in new window


i am using this code, your version of the code just runs through the code and no message comes up on the screen.
Aravind RanganathanWindows Application DeveloperAuthor Commented:
This code finally worked for some reason it likes it when you manually pass the values of the array into the alert:

 Response.Write("<script language=\"javascript\">alert('" + message[0] + "' + '\\n' + '" + message[1] + "' + '\\n' + '" + message[2] + "' + '\\n' + '" + message[3] + "' + '\\n' + '" + message[4] + "' + '\\n' + '" + message[5] + "');</script>");

Open in new window

Miguel OzSoftware EngineerCommented:
OK it seems extra backslash is needed for new line (\\n), please use the following code:
string script = "alert('" + string.Join("\\n", message.ToArray()) + "');";

Open in new window


out of curiosity what version of C#, VS and ASP.NET are you using?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aravind RanganathanWindows Application DeveloperAuthor Commented:
@Miguel Oz i am using C# 4.0 released with .NET 4
Miguel OzSoftware EngineerCommented:
Thanks, my latest code should work OK. I am assuming you are using ASP.NET not ASP.NET MVC.
Aravind RanganathanWindows Application DeveloperAuthor Commented:
Thanks Bud
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.