Sunday, February 25, 2007
 At work I decided to add Ajax 1.0 for ASP.NET to a few of my web pages. With not a lot of time to dig into it, I wanted something simple and fast to use at first. Later, I could dig into using the more sophisticated features of Ajax.  

The UpdatePanel along with the UpdateProgress control seemed to be the perfect solution. Both work just like a standard panel control that holds other web controls like a container.

These extensions are really slick, but I ran into a few issues with Response.Write

My code has a routine that spawns an java alert box to display any errors messages. This is pretty standard code and will display a small popup window to output your message. This is pretty much a standard way of displaying a quick message to the user.  Nothing very special about this, except that it doesn't work with the new Ajax extensions. smile_sad 

 

   1:  Protected Sub PopErrorWindow(ByVal ErrMsg As String) 
   2:   
   3:        Response.Write("<script language='javascript'>window.alert('" & ErrMsg & "');</script>") 
   4:   
   5:  End Sub

 

After embedding my controls within the update panel, I started getting the following error from the Response.Write in the code above. It seems that Ajax doesn't like the partial postback that I was doing.

I talked it over with a co-worker and after a bit of Internet searching and experimenting around with code, I was able to come up with the following code as a solution. Seems to work fine and I haven't found any other side effects.  smile_teeth

 

   1:  Protected Sub PopErrorWindow(ByVal ErrMsg As String)
   2:   
   3:          ScriptManager.RegisterClientScriptBlock(Me.UpdatePanel1, Me.GetType, "Alert", "alert('" + ErrMsg + "')", True)
   4:         
   5:   End Sub

 

I hope this helps anyone else out there having a similar problem.

posted on Sunday, February 25, 2007 9:39:09 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1]
Related posts:
VS 2008 & .NET 3.5 SP1 Beta
Free Software for Students
Heroes Happen Here Comic
.NET Graphical Resources
Create an Access Database in C#
Organize Usings