Share via


Response.Write Alert box in C#

Question

Monday, December 4, 2006 1:34 PM

I'm using the following code to output an error message to the user via a javascript alert box:

Response.Write("<script language='javascript'>alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");

I need a line break after the first sentence. ('The following errors have occurred:)

This code works in VB .NET (minus the semicolon) but not in C#.  The problem is the line break character "\n".

How do I make this work in C#?

Thanks in advance.

 

All replies (15)

Monday, December 4, 2006 1:57 PM âś…Answered

You forgot the @ sign 

Response.Write(@"<script language='javascript'>alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>"); 
or you can double escape the \n by doing 
... occurred: \\n" + strErrorDesc...
 

 


Monday, December 4, 2006 2:11 PM

Change \n to <BR>


Monday, December 4, 2006 2:45 PM

Thanks haoest,

 It's the little things between the two languages that kill.  Your suggestions worked.

Thanks,


Tuesday, May 22, 2007 10:26 AM

I have been trying to do the exact thing and I kept getting this error? Below is my code.

Response.Write(@"<script language='javascript'>alert('Update is successful.')</script>");

Any idea why I kept getting that error?

Thanks in advance.


Tuesday, May 22, 2007 11:14 AM

Okay, I just found out what the problem is from another thread that points to the the AJAX UpdatePanel control I am using. If I turn off the UpdatePanel then the Javascript alert box works just fine. Any other suggestion on how to resolve this?


Monday, May 28, 2007 7:55 AM

hi

you can also try for Page.RegisterClientScriptBlock();

 and

Page.RegisterStartupScript () method for emiting javascript in page html.

 thanks

vishal


Monday, June 4, 2007 9:10 AM

hi

you can also try for Page.RegisterClientScriptBlock();

 and

Page.RegisterStartupScript () method for emiting javascript in page html.

 thanks

vishal

Will you give me an example code on how to use it?


Monday, June 4, 2007 9:32 AM

When I search in MSDN on both of your suggestion and this is what I get.

NOTE: This method is now obsolete.  

 


Tuesday, June 5, 2007 5:33 AM

hi

i was unaware about that thing , microsoft has made that method obsolete .

but it has given it well meaning , Page.ClientScript.

read out more about this at msdn , it will handle your script at client side.

and for while below is a short example

Write the following code in your code behing page load :

  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    String csname2 = "ButtonClickScript";
    Type cstype = this.GetType();
        
    // Get a ClientScriptManager reference from the Page class.
    System.Web,UI.ClientScriptManager cs = Page.ClientScript;

    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
      String cstext1 = "alert('Hello World');";
      cs.RegisterStartupScript(cstype, csname1, cstext1, true);
    }

    // Check to see if the client script is already registered.
    if (!cs.IsClientScriptBlockRegistered(cstype, csname2))
    {
      StringBuilder cstext2 = new StringBuilder();
      cstext2.Append("<script type=text/javascript> function DoClick() {");
      cstext2.Append("Form1.Message.value='Text from client script.'} </");
      cstext2.Append("script>");
      cs.RegisterClientScriptBlock(cstype, csname2, cstext2.ToString(), false);
    }
  }

and type the following in HTML part. 

  <body>
     <form id="Form1"
         runat="server">
        <input type="text" id="Message"> <input type="button" value="ClickMe" onclick="DoClick()">
     </form>
  </body>
</html>

 

try this.

thanks

vishal

Tuesday, June 5, 2007 9:22 AM

I already tried this:

 

1    ClientScriptManager csm = Page.ClientScript;
2    
3    if(!csm.IsStartupScriptRegistered(this.GetType(), "winPop"))
4    {
5       csm.RegisterStartupScript(this.GetType(), "winPop", "alert('Update is successful');", true);
6    }

  

And it is not working. Again, I have to stress that the reason why it does not work is because I'm using AJAX UpdatePanel. If I turned off the AJAX UpdatePanel then it works perfectly. So unless there is a trick going around this AJAX UpdatePanel issue, it's not going to work.


Friday, November 9, 2007 2:15 PM

Did you figure out a solution to this problem? I have exactly the same issue.....trying to figure out how to fire an alert while using AJAX 


Tuesday, February 17, 2009 8:35 PM

hi,

old thread but i thought i'd post...might help somebody out...

for ajax, you need to use the scriptmanager's built in method: RegisterClientScriptBlock

 eg. ScriptManager.RegisterClientScriptBlock(UpdatePanelControlName, typeof(UpdatePanel), "somerandomstring", "alert('yo');", true);

good luck...

mick.


Friday, May 29, 2009 4:21 AM

 the following code work fine in c# under code behind with @ symbol in proper place

"<script language='javascript'>alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");

 

MY ISSUE IS I NEED A NEW LINE AFTER strErrorDesc for that i used a modified code like this 

                        Response.Write(@"<script language='javascript'>alert('RESETTER PFNO: \n" + Session["usrid"].ToString() + " \n BELONGS TO AN OFFICER OF CPPC');</script>");

 

BUT IT DIDN'T WORKED AT ALL 

SO COULD YOU PLEASE GUIDE ME TO SOLVE THIS SITUATION

 output i require is

RESETTER PFNO:

20154 

BELONGS TO AN OFFICER OF CPPC

 


Saturday, May 30, 2009 1:13 AM

Hello try this one.

 alert('First Line\nSecond Line') ;

hope this helps!

thanks!

 


Saturday, May 30, 2009 1:35 AM

 it is working.......

Response.Write(@"<script language='javascript'>alert('RESETTER PFNO: \n" + strErrorDesc + " \n BELONGS TO AN OFFICER OF CPPC');</script>");

 

my articles