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

Executing script after Ajax returns

1 Answer 463 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Barry Katz
Top achievements
Rank 1
Barry Katz asked on 13 Aug 2008, 12:11 PM
I have an ASP panel that contains a button. The button acts as the ajax initiator. When clicked, the button's server click event needs to run a javascript that is outside of the panel. I had to put the javascript outside of the panel because other controls outside of the panel also need to run the script. I decided to use a Literal control and simply put the javascript into the Text property in the button's click event. I am using RadAjaxManager. I thought I could then have the button act as an initiator for the literal control but that didn't work. Even putting the control inside of a PlaceHolder didn't work.

The script never gets executed. If I turn off Ajax, it does get executed.

When I read the documentation on RadAjax, it only says the following:

"RadScriptBlock is used where you have JavaScript that evaluates after an AJAX request, for example when the content of RadAjaxPanel is updated asynchronously. RadScriptBlock also can be used like RadCodeBlock to handle server code blocks (<% ... %>)."

What is not clear here is how this RadScriptBlock applies when you are NOT using a RadAjaxPanel. In any event, even after I add a RadScriptBlock, the script never runs.

The Microsoft documentation also indicates the following:

"If you want to register a script block that does not pertain to partial-page updates, and if you want to register the script block only one time during initial page rendering, use the RegisterClientScriptBlock method of the ClientScriptManager class."

The script I am trying to update is not what I consider a partial-page update. Either it should update always whenever any Ajax call is made or only when I want it to, such as in the button's click event. The confusion seems to be understanding when to use Microsoft's built-in Ajax functions and when to rely on Telerik's. Can someone please indicate how to solve my problem as I have spent hours with no success. Preferrably someone at Telerik should comment here on this issue because Telerik is clearly implementing a mixture of Microsoft's Ajax along with their own support mechanisms.

Thank you for your help
Johann

1 Answer, 1 is accepted

Sort by
0
Missing User
answered on 14 Aug 2008, 02:10 PM
Hello Barry,


RadScriptBlock is used where you have JavaScript that evaluates after an AJAX request, for example when the content of RadAjaxPanel is updated asynchronously. RadScriptBlock wraps JavaScript where the JavaScript is located in an updating area. Here is an example:

<%@ Page Language="C#"  %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
    <script type="text/javascript"
    function MyAlert() 
    { 
        alert("MyAlert"); 
    } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <div> 
 
             <telerik:RadAjaxPanel ID="RadAjaxPanel2" runat="server">  
               <asp:Panel ID="Panel1" runat="server">  
                   <asp:Button ID="Button2" runat="server" Text="Button1" />  
                   <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">  
                       <script type="text/javascript">  
                           MyAlert(); 
                       </script>  
                   </telerik:RadScriptBlock>  
               </asp:Panel>  
            </telerik:RadAjaxPanel>               
 
        </div> 
    </form> 
</body> 
</html> 
 




You use the RegisterClientScriptBlock method to register a client script block that is compatible with partial-page rendering and that has no Microsoft AJAX Library dependencies. Client script blocks that are registered by using this method are sent to the page only when control represents a control that is inside an UpdatePanel control that is being updated. To register a script block every time that an asynchronous postback occurs, use the RegisterClientScriptBlock(Page, Type, String, String, Boolean) overload of this method.  Here is an example:

<%@ Page Language="C#"  %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
 
<script runat="server"
 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "MyAlert()", true); 
    } 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
    <script type="text/javascript"
    function MyAlert() 
    { 
        alert("MyAlert"); 
    } 
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <div> 
            <telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="200px" Width="300px"
                <asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" /> 
            </telerik:RadAjaxPanel> 
             
 
        </div> 
    </form> 
</body> 
</html> 
 


All the best,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Ajax
Asked by
Barry Katz
Top achievements
Rank 1
Answers by
Missing User
Share this question
or