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

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

1 Answer 376 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Polaris431
Top achievements
Rank 1
Polaris431 asked on 21 Jan 2009, 04:44 PM
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?

1 Answer, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 26 Jan 2009, 10:19 AM
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.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server">  
        <asp:ScriptManager ID="ScriptManager1" runat="server">  
        </asp:ScriptManager> 
        <div> 
            <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">  
                <AjaxSettings> 
                    <telerik:AjaxSetting AjaxControlID="Button1">  
                        <UpdatedControls> 
                            <telerik:AjaxUpdatedControl ControlID="Button1" /> 
                        </UpdatedControls> 
                    </telerik:AjaxSetting> 
                </AjaxSettings> 
            </telerik:RadAjaxManager> 
            <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">  
            <script type="text/javascript">  
            function Click()  
            {  
                $find("<%= RadAjaxManager1.ClientID %>").ajaxRequestWithTarget("<%= Button1.UniqueID %>", "");  
                return false;  
            }  
            </script> 
            </telerik:RadCodeBlock> 
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="return Click();" /></div>  
    </form> 
</body> 
</html> 
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
 
public partial class Default6 : System.Web.UI.Page  
{      
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "alert""alert('StartupScript');"true);  
        RadAjaxManager1.ResponseScripts.Add("alert('ResponseScripts');");  
    }  
 

All the best,
Iana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Ajax
Asked by
Polaris431
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or