Why does ResponseScripts.Add cause script to execute but ClientScript.RegisterStartupScript doesn't?

2 posts, 0 answers
  1. Polaris431
    Polaris431 avatar
    30 posts
    Member since:
    Jul 2007

    Posted 21 Jan 2009 Link to this post

    I have a RadToolbar that implements the ButtonClick event on the server. Some toolbar buttons will cause the ButtonClick event to execute automatically when the buttons are pressed while other buttons must be first intercepted with javascript code and determined if the ButtonClick event will be executed on the server or not. If the javascript code does execute the ButtonClick event, it does this by using the ajaxRequestWithTarget method on the RadAjaxManager object. This all works fine. In the ButtonClick event, prior to exiting the method, I need to create a client-side script that needs to get executed once the page has been processed. I have tried virtually every combination of RegisterStartupScript either from the ScriptManager object or ClientScript object but the script never gets executed. I have even tried relocating the script to the start of the page, at the end of page, inside Literals, etc. - but it never executes. If I use the ResponseScripts.Add method from the RadAjaxManager object to add the script, the script will run. Why?? When I read the documentation on the RegisterStartupScript in documentation, there is no mention that it requires Ajax functionality, let alone something from Telerik. If the ButtonClick event is being called by ajaxRequestWithTarget, is this somehow preventing Microsoft's ScriptManager or ClientScript object from executing the script?
  2. Iana Tsolova
    Iana Tsolova avatar
    3388 posts

    Posted 26 Jan 2009 Link to this post

    Hello Polaris431,

    The ScriptManager.RegisterStartupScript() and RadAjaxManager ResponseScripts.Add() methods did executed the client code added in the Click event handler of a button control. 
    Try the below sample and see if it works on your end.

    1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %> 
    3 <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
    4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    5 <html xmlns="http://www.w3.org/1999/xhtml">  
    6 <head runat="server">  
    7     <title>Untitled Page</title> 
    8 </head> 
    9 <body> 
    10     <form id="form1" runat="server">  
    11         <asp:ScriptManager ID="ScriptManager1" runat="server">  
    12         </asp:ScriptManager> 
    13         <div> 
    14             <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">  
    15                 <AjaxSettings> 
    16                     <telerik:AjaxSetting AjaxControlID="Button1">  
    17                         <UpdatedControls> 
    18                             <telerik:AjaxUpdatedControl ControlID="Button1" /> 
    19                         </UpdatedControls> 
    20                     </telerik:AjaxSetting> 
    21                 </AjaxSettings> 
    22             </telerik:RadAjaxManager> 
    23             <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">  
    24             <script type="text/javascript">  
    25             function Click()  
    26             {  
    27                 $find("<%= RadAjaxManager1.ClientID %>").ajaxRequestWithTarget("<%= Button1.UniqueID %>", "");  
    28                 return false;  
    29             }  
    30             </script> 
    31             </telerik:RadCodeBlock> 
    32             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="return Click();" /></div>  
    33     </form> 
    34 </body> 
    35 </html> 
    1 using System;  
    2 using System.Data;  
    3 using System.Configuration;  
    4 using System.Collections;  
    5 using System.Web;  
    6 using System.Web.Security;  
    7 using System.Web.UI;  
    8 using System.Web.UI.WebControls;  
    9 using System.Web.UI.WebControls.WebParts;  
    10 using System.Web.UI.HtmlControls;  
    12 public partial class Default6 : System.Web.UI.Page  
    13 {      
    14     protected void Button1_Click(object sender, EventArgs e)  
    15     {  
    16         ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert""alert('StartupScript');"true);  
    17         RadAjaxManager1.ResponseScripts.Add("alert('ResponseScripts');");  
    18     }  

    All the best,
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Back to Top