Will "the Mighty" Strohl

Microsoft AJAX 1.0 - Common Error

I began playing with the Microsoft AJAX 1.0 features today with an actual project instead of simple test scenarios similar to the AJAX videos offered on the ASP.Net web site. I am using AJAX controls to make an asynchronous call to my server which in turns calls an external web service and repopulates a textarea control. The first click of the button that submits the call works perfectly. The call goes out, and in just 'sec my SOAP response is shown in the textarea without a hitch. However, this is where a big brick gets thrown through the window!

If you click the button again without refreshing the page or removing the text from the textarea, an AJAX error will be thrown in the form of an alert that states:

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

Now this was a perplexing error since us .Net developers have become so accustomed to reading errors that include some details about how and where the error occurred. That information would undoubtedly leads us to the "why" of the error. However, this alert doesn't give us a whole lot to go on to troubleshoot the problem.

I proceeded to do what nearly all of us do... I Googled the error. I had a handful of appropriate results show up, but most of them were outdated and none of them had a single clear answer as to how or why this happened. I still do not know the how of my error, but I was able to determine the the why with a few hints from a couple of the search results.

After the first request was sent out as a result of clicking the button, the textarea was loaded with an XML web service response. This obviously consisted of HTML characters like the "greater than" and "less than" symbols. If I attempted to send a subsequent request by clicking the button a second time WITHOUT reloading the page, the error would occur because of the HTML in the textarea. Since the textarea is located within an UpdatePanel, the text within the textarea, I am assuming the error occurs because the HTML could contain harmful content.

Now, I am sure that there is a much more elegant and event easier fix to this, but I have not thoroughly investigated this yet. The quick fix I found for this was to inject a JavaScript snippet into the OnClick event of the button. This would look something like the following:

Dim strScript As String = "document.getElementById('" & _
Me.txtResponse.ClientID & "').value = '';"
Me.cmdSubmit.Attributes.Add("onclick", strScript)

This clears out any text in the textarea before the request is actually sent from the browser to the web service. Now, I said before that this is probably not the most efficient means to do this. I only did this because it is for a test application that is testing my web service. I wouldn't do this for a production application. But this workaround may do something for you in a situation like this.

If you have the actual fix or want to tell me how horrible of a coder I am, leave some comments. :) Until next time...

blog comments powered by Disqus