Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to concatenate a string variable inside alert box but its not working. When I write text inside this alert box without concatenating this variable it works. Below is my code
C#
string valid = "";
if (FileUpload1.HasFile)
                {
                    string extension = Path.GetExtension(FileUpload1.FileName);
                    if (extension == ".jpg" || extension == ".jpeg" || extension == ".png")
                    {
                        string no = Guid.NewGuid().ToString().Substring(0, 4);
                        string path = @"~/Admin/Logos/";
                        string serverpath = Server.MapPath(path);
                        string name = FileUpload1.FileName;
                        string fullpath = serverpath + no + name;
                        FileUpload1.SaveAs(fullpath);
                        dbpath = path + no + name;
                        
                    }
                    else
                    {
                        valid = "File extension should be jpg,jpeg or png\n";
                    }
                }
                else
                {
                    valid = "Please choose a logo to upload\n";
                }

                if (valid != "")
                {
                    ClientScript.RegisterStartupScript(typeof(Page), "script", "alert('"+valid+"');", true);
                }

Thanks..
Posted
Comments
Naz_Firdouse 8-Apr-13 4:16am    
please check this...
string customerNo = "28272";
string ErorMessage = "cannot be found";
ClientScript.RegisterStartupScript(typeof(Page), "script", "alertMessage('" + customerNo + "','"+ ErorMessage +"');", true);

The link is
http://forums.asp.net/t/1654753.aspx/1

I have got a similar thing working on one of my own pages. Try losing the single quotes from inside the alert statement and just pass the variable directly:

C#
ClientScript.RegisterStartupScript(typeof(Page), "script", "alert(" + valid + ");", true);


EDIT: on second thought, this only seems to work if 'valid' contains a numeric value, not a string. See Pallavi's solution for the correct answer.
 
Share this answer
 
v2
it is happening because of \n
use
valid = "File extension should be jpg,jpeg or png";

at the place of
valid = "File extension should be jpg,jpeg or png\n";
 
Share this answer
 

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