This is a migrated thread and some comments may be shown as answers.

RadAjaxPanel.ResponseScript.Add doesnt work

4 Answers 360 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 1
Dan asked on 17 Jul 2011, 04:06 AM
I am trying to show the radalert dialog box using the AjaxPanel.ResponseScript.add function.  Whats interesting is that it does work if I write static text in the radalert function, but I want the error message to display back to the user.

 
<VB.NET>
-------------------

 

 

Catch ex As Exception

 

 

 

    Me.RadAjaxPanel1.ResponseScripts.Add("radalert('" & ex.Message & "')")  '' < Doesn't work>

 

 

 

    Me.RadAjaxPanel1.ResponseScripts.Add("radalert('A error occurred. ')")  '' < Works >

 

 

 

End Try

 


I want the radalert to dynamically display the text of the error message  in the dialog box.  I dont want to have static text.  What am I doing wrong?

4 Answers, 1 is accepted

Sort by
0
Genti
Telerik team
answered on 18 Jul 2011, 02:30 PM
Hi Dan,

Did you include a RadWindowManager on the page?
The declarations should look like this:
<telerik:RadWindowManager ID="WindowManager1" runat="server"></telerik:RadWindowManager>
 
<telerik:RadAjaxPanel runat="server" ID="RadAjaxPanel1">
    <asp:Button Text="Get Response" ID="btn1" OnClick="btn1_Click" runat="server" />        
</telerik:RadAjaxPanel>

And in the code-behind:
protected void btn1_Click(object sender, EventArgs e)
{
    string someValue="Something";
    RadAjaxPanel1.ResponseScripts.Add("radalert('" + someValue + "')");
}


Best wishes,
Genti
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Dan
Top achievements
Rank 1
answered on 19 Jul 2011, 07:14 PM
Originally I had the the RadWindowManager below and outside the RadAjaxPanel.  I moved the RadWindowManager above (still outside) the panel, but still no radalert shows when trying to dynamically include text based on the exception.  Again, if I manully place text in the radalert (example: radalert('static text')), its appears.  So I wouldn't think it has to do with the placement of the RadWindowManager, although I did try your suggestion. 
0
Genti
Telerik team
answered on 20 Jul 2011, 09:28 AM
Hello Dan,

The client error handling should be implemented through the scriptmanager instance.
In order to do so you have to make the following changes.

1) Define a AsyncPosbackError handler:
<telerik:RadScriptManager ID="RadScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
Code behind:
protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
    RadScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message;
}

2) Then in order to get the response from the ScriptManager on the client you can do the following(JavaScript):
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
 
        function EndRequestHandler(sender, args) {
 
            if (args.get_error() != undefined && args.get_error().httpStatusCode == '500') {
                var errorMessage = args.get_error().message
                args.set_errorHandled(true);
                radalert(errorMessage);
            }
 
        }

I am also attaching a simple project that illustrates this.


Best wishes,
Genti
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Dan
Top achievements
Rank 1
answered on 20 Jul 2011, 05:06 PM

Solution.

You suggestions helped in finding the problem, but the basic problem was this:  The radalert text argument can not have a value with either character returns or line breaks in them.  Basically, what happen was, the exception.message value had a line break in the value.  This was apparent when, during troubleshooting, I used the msgbox in the code behind to display the error message (see attachment).  I created a sub routine to change the characters to thier equivalent ASC value.  When I did that, I saw 10 and 13 which are line feeds and character return codes.  The sub routine removes those codes, and when I used the returned value from my sub in the radalert function everything worked.

Public Function RemoveLineBreaks(ByVal text As String) As String
  
        Dim Length As Integer = Len(text)
        Dim i As Integer = 1
        Dim Asc As String
        Dim AscStringBuilder As New StringBuilder
        Dim AscString As String
        Dim AscArray() As String
        Dim TextBuilder As New StringBuilder
  
  
        'Convert to ASC String
        AscStringBuilder.Append("-")
        Do Until i > Length
            Asc = Strings.Asc(Strings.GetChar(text, i).ToString)
            AscStringBuilder.Append(Asc & "-")
            i = i + 1
        Loop
  
        'Replace LineBreak codes
        AscString = Replace(Replace(AscStringBuilder.ToString, "-10-", "-"), "-13-", "-")
  
  
        'Convert back to text
        AscArray = Split(AscString, "-")
  
        For Each AscNumber In AscArray
            If IsNumeric(AscNumber) Then
                TextBuilder.Append(Strings.Chr(AscNumber).ToString)
            End If
        Next
  
  
        Return TextBuilder.ToString()
  
    End Function
  
  
End Module


Tags
Ajax
Asked by
Dan
Top achievements
Rank 1
Answers by
Genti
Telerik team
Dan
Top achievements
Rank 1
Share this question
or