Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET
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
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 7-Apr-13 23:04pm
Comments
Naz_Firdouse at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 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:
 
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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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";
  Permalink  

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

  Print Answers RSS
0 Maciej Los 315
1 OriginalGriff 273
2 Aajmot Sk 224
3 Marcin Kozub 210
4 Richard MacCutchan 200
0 OriginalGriff 7,853
1 Sergey Alexandrovich Kryukov 7,107
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 8 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100