2

I am showing alert box in my page, but after that my page is breaking down.

Response.Write("<script>alert('Selected items are removed successfully')</script>");

How to fix that?

jrummell
  • 42,637
  • 17
  • 112
  • 171
user1285383
  • 73
  • 2
  • 2
  • 4
  • 4
    _my page is breaking down_ - you'll need to be much more specific. – jrummell Jun 12 '12 at 14:58
  • 2
    Think about what a mechanic would ask if you strolled in the door and asked: "My car is broken down, what should I do?". Be specific with your problem and describe the symptoms. – Jeremy Jun 12 '12 at 14:59

3 Answers3

7

That's not the way to send javascript code to client on ASP.NET

You could use Page.ClientScript.RegisterStartupScript

 Page.ClientScript.RegisterStartupScript(
   this, 
   GetType(), 
   "ALERT", 
   "alert('Selected items are removed successfully')", 
   true);

In this case, you could also use Page.ClientScript.RegisterClientScriptBlock

 Page.ClientScript.RegisterClientScriptBlock(
   this, 
   GetType(), 
   "ALERT", 
   "alert('Selected items are removed successfully')", 
   true);

To understand differences between RegisterStartupScript and RegisterClientScriptBlock you could check here

Difference between RegisterStartupScript and RegisterClientScriptBlock?

You will also understand why they aren't always interchangeable.

Community
  • 1
  • 1
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
2

Response.Write writes directly to the output stream, before the page is rendered. If you look at the resulting page code, you will see your output before any HTML. This breaks page state among other things.

What you most likely want is something like the RegisterStartupScript method, to write out a proper script block at the end of the page.

Link to documentation: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx

See @Claudio Redi's post for a code example of this method.

dodexahedron
  • 4,584
  • 1
  • 25
  • 37
1

Here's the syntax that triggers javascript code during a webform post-back:

    Dim scVal As String
    scVal = "<script type='text/javascript'>"
    scVal = scVal & " alert('Selected items are removed successfully');</script>"
    ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "temp", scVal, False)
user652411
  • 211
  • 3
  • 11