Home / Community & Support / Knowledge Base / RadControls for ASP.NET AJAX / Window / Calling radalert from codebehind (all versions of RadWindow)

Calling radalert from codebehind (all versions of RadWindow)

Article Info

Rating: 2

 

Article information

Article relates to

all versions of RadWindow

Created by

Georgi, Telerik

Last modified

January 5th, 2012

Last modified by

Marin, Telerik



HOW-TO

Call radalert() from codebehind.






NOTE:
Since Q1 2011 (2011.1.315) the RadWindowManager offers three built-in server-side methods that allow you to call a RadAlert, RadConfirm and RadPrompt from the server. More information about their syntax is available in this online demo which also shows them in action. 



When calling the radalert() function server-side in ASP.NET AJAX environment, you should keep in mind that the page lifecycle is different. RadControls for ASP.NET AJAX, being MS AJAX objects, are rendered after page is loaded (e.g. after window.onload is fired). Therefore executing code in the window.onload event doesn't ensure that the RadWindowManager is already rendered on the page and you need to add a Sys.Application.Load handler. We however, must make sure that the code is executed only once and then removed from the Load handler - otherwise it will be called after every Ajax callback. One way to do this is to use the following logic :

function f() 
//code 
Sys.Application.remove_load(f); 
Sys.Application.add_load(f); 

The attached examples show how to do this in the following scenarios:
  1. Calling radalert() on postback. In this case you can use the RegisterStartupScript() server method if you are using ASP.NET AJAX controls (used in the example).

     ASPX
    <form id="form1" runat="server">  
            <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
                <telerik:RadWindowManager ID="RadWindowManager1" runat="server">  
                </telerik:RadWindowManager> 
                <asp:Button ID="Button1" Text="Postback and show RadAlert" runat="server"  OnClick="Button1_Click" /> 
    </form> 

    C#
    protected void Button1_Click(object sender, EventArgs e) 
        string radalertscript = "<script language='javascript'>function f(){radalert('Welcome to RadWindow <b>Prometheus</b>!', 330, 210); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>"
        Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript); 

    VB.NET
    Protected Sub Button1_Click(sender As Object, e As EventArgs) 
        Dim radalertscript As String = "<script language='javascript'>function f(){radalert('Welcome to RadWindow <b>Prometheus</b>!', 330, 210); Sys.Application.remove_load(f);}; Sys.Application.add_load(f);</script>" 
        Page.ClientScript.RegisterStartupScript(Me.[GetType](), "radalert", radalertscript) 
    End Sub 
  2. Calling radalert() on ASP.NET AJAX callback: Use the RegisterStartupScript() server method of the ScriptManager to achieve the desired behavior:

     ASPX
    <form id="form1" runat="server">  
            <asp:ScriptManager ID="ScriptManager1" runat="server">  
            </asp:ScriptManager> 
            <telerik:RadWindowManager ID="RadWindowManager1" runat="server">  
            </telerik:RadWindowManager> 
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
                <ContentTemplate> 
                    <asp:Button ID="Button1" Text="CallBack and show RadAlert" runat="server" OnClick="Button1_Click" /> 
                </ContentTemplate> 
            </asp:UpdatePanel> 
        </form> 

    C#
     protected void Button1_Click(object sender, EventArgs e)  
        {  
            string scriptstring = "radalert('Welcome to Rad<b>window</b>!', 330, 210);";  
            ScriptManager.RegisterStartupScript(thisthis.GetType(), "radalert", scriptstring, true);  
        } 

    VB.NET
     Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs)  
            Dim scriptstring As String = "radalert('Welcome to Rad<b>window</b>!', 330, 210);" 
            ScriptManager.RegisterStartupScript(MeMe.[GetType](), "radalert", scriptstring, True)  
        End Sub 
     
  3. Calling radalert on RadAjax callback: You can use the ResponseScripts collection of RadAjaxManager for this purpose:

    ASPX
    <form id="form1" runat="server"
    <asp:ScriptManager ID="ScriptManager1" runat="server"
    </asp:ScriptManager> 
    <telerik:RadWindowManager ID="RadWindowManager1" runat="server"
    </telerik:RadWindowManager> 
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"
        <AjaxSettings> 
            <telerik:AjaxSetting AjaxControlID="Button1"
                <UpdatedControls> 
                    <telerik:AjaxUpdatedControl ControlID="RadWindowManager1" /> 
                </UpdatedControls> 
            </telerik:AjaxSetting> 
        </AjaxSettings> 
    </telerik:RadAjaxManager> 
    <asp:Button ID="Button1" Text="Callback and show RadAlert" runat="server" OnClick="Button1_Click" /> 
    </form> 

    C#
    protected void Button1_Click(object sender, EventArgs e) 
        RadAjaxManager1.ResponseScripts.Add(@"radalert('Welcome to RadWindow <b>Prometheus</b>!', 330, 210);"); 

    VB.NET
    Protected Sub Button1_Click(sender As Object, e As EventArgs) 
        RadAjaxManager1.ResponseScripts.Add("radalert('Welcome to RadWindow <b>Prometheus</b>!', 330, 210);"
    End Sub 


The main idea in calling radalert() server-side when using the RadControls for ASP.NET RadWindowManager stays the same excluding Sys.Application.load event, because this suite is not built upon the MS AJAX Framework.

The attached examples show how to do this in the following scenarios:
  1. Calling radalert() on postback. In this case you can use the RegisterStartupScript() server method if you are using ASP.NET v2.x (used in the example).
    ASPX
    <form id="form1" runat="server">  
        <radW:RadWindowManager ID="RadWindowManager1" runat="server">  
        </radW:RadWindowManager> 
        <asp:Button ID="Button1" Text="Postback and show RadAlert" runat="server" OnClick="Button1_Click" />&nbsp;  
    </form> 

    C#
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        string radalertscript = "<script language='javascript'> window.onload = function(){radalert('Welcome to Rad<b>window</b>!', 330, 210);}</script>";  
        Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript);  

    VB.NET
    Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs)  
            Dim radalertscript As String = "<script language='javascript'> window.onload = function(){radalert('Welcome to Rad<b>window</b>!', 330, 210);}</script>" 
            Page.ClientScript.RegisterStartupScript(Me.[GetType](), "radalert", radalertscript)  
    End Sub 


  2. Calling radalert() on ASP.NET AJAX callback: Use the RegisterStartupScript() server method of the ScriptManager to achieve the desired behavior:
    ASPX
    <asp:ScriptManager ID="ScriptManager1" runat="server">  
    </asp:ScriptManager> 
    <radW:RadWindowManager ID="RadWindowManager1" runat="server">  
    </radW:RadWindowManager> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
        <ContentTemplate> 
            <asp:Button ID="Button1" Text="CallBack and show RadAlert" runat="server" OnClick="Button1_Click" /> 
        </ContentTemplate> 
    </asp:UpdatePanel> 

    C#
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        string scriptstring = "radalert('Welcome to Rad<b>window</b>!', 330, 210);";  
        ScriptManager.RegisterStartupScript(thisthis.GetType(), "radalert", scriptstring, true);  

    VB.NET
    Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs)  
        Dim scriptstring As String = "radalert('Welcome to Rad<b>window</b>!', 330, 210);" 
        ScriptManager.RegisterStartupScript(MeMe.[GetType](), "radalert", scriptstring, True)  
    End Sub 
  3. Calling radalert on RadAjax callback: You can use the ResponseScripts collection of RadAjaxManager for this purpose:
    ASPX

    <form id="form1" runat="server"
      <radW:RadWindowManager ID="RadWindowManager1" runat="server"
      </radW:RadWindowManager> 
      <radA:RadAjaxManager ID="RadAjaxManager1" runat="server"
          <AjaxSettings> 
             <radA:AjaxSetting AjaxControlID="Button1"
                <UpdatedControls> 
                   <radA:AjaxUpdatedControl ControlID="RadWindowManager1" /> 
                </UpdatedControls> 
             </radA:AjaxSetting> 
          </AjaxSettings> 
      </radA:RadAjaxManager> 
      <asp:Button ID="Button1" Text="Callback and show RadAlert" runat="server" OnClick="Button1_Click" /> 
    </form> 

     
    C#
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        RadAjaxManager1.ResponseScripts.Add("radalert('Welcome to Rad<b>window</b>!', 330, 210);");  

    VB.NET
    Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs)  
        RadAjaxManager1.ResponseScripts.Add("radalert('Welcome to Rad<b>window</b>!', 330, 210);")  
    End Sub 

 
Depending on the case, your exact scenario might differ from the ones used in the examples - in this case you can modify the code to fit your requirements. The main idea stays the same:

  1. Make sure that the JavaScript is output on the page.
  2. Make sure that the RadWindowManager is rendered on the page before you execute the JavaScript.

Comments

  • Josef Rogovsky , Oct 25, 2006

    This doesn't work when the controls are inside a rad.ajaxpanel. Any suggestions for that situation?

  • Telerik Admin , Nov 8, 2006

    Josef, this code does not work with ajax because we are calling the radalert on pageload. If you need assistance getting this to work with r.a.d.ajax, open a support ticket and we will assist you.

  • Evgeny Vdovin , Feb 6, 2007

    Great job.

  • fatima , Jul 30, 2007

    Dont just tell us to open a support ticket! This is supposed to be a knowledge base, not a delegation station!! Use it as such!

  • Telerik Admin , Sep 28, 2007

    The KB article has been extended to cover 3 common scenarios when calling radalert() from codebehind.

  • Sam Langdon , Apr 17, 2008

    Hi Guys, This all works fine, but unfortunately the alert is now stuck in the response scripts and will be shown every time the ajax panel reloaded. Is there a way to clear it once it has been shown?

  • Kevin Pipher , Apr 25, 2008

    Great article. I've used it to create a "RegisterStartupScript" method in a base masterpage class that determines what type of callback/postback is occurring (radajax, msajax, or postback) and uses the appropriate register call as described above.

  • Peter Luth , Jun 12, 2008

    Kevin, could you share your code which determines radajax,msajax or postback?

  • Telerik Admin , Jun 26, 2008

    Hi Sam,
    Indeed, if it is added in the reposnsescripts collection, the script will be executed. That is why you need to clear it right after the execution.

  • Sean , Aug 12, 2008

    I tried to clear the ResponseScripts by using the following command: RadAjaxManager1.ResponseScripts.Clear(); The Alert is still showing...

  • Sean , Aug 13, 2008

    In case you are interested, the following line should be used in method 3 to solve the problem of the alert becoming stuck in the response scripts: RadAjaxManager1.ResponseScripts.Add("(function(){var f = function(){radalert('Welcome to RadWindow Prometheus!', 330, 210);Sys.Application.remove_load(f);};Sys.Application.add_load(f);})()");

  • Peter Luth , Sep 1, 2008

    Hi Ricardo, I tried using your reload method and it helped me out with multiple alerts showing on the screen, but unfortunately it doesn't show on the first callback. Is there a link to an example of using this in a Masterpage/contentpage scenario? Mark.

  • jo , Sep 15, 2008

    I'm having the same issue for clearing the responsescripts. and showing alert on first callback.

  • Juan Angel , Sep 18, 2008

    Muchas gracias Ricardo, funciona perfectamente. Salu2.

  • J2K , Oct 15, 2008

    so many problems with this approach it would take a book to describe .... not useful at all

  • J2K , Oct 15, 2008

    when one has to go to this much trouble for a simple alert box, it totally defeats the goal of keeping code concise and error free in a variety of browser environments .... a better solution would require one line in code: radwindow.alert("Telerik needs to rethink its model")

  • Telerik Admin , Oct 17, 2008

    Hi Mark,
    Actually the approach is not much different than calling a standard browser's alert on the client. Unlike Winforms when working with Web, the alert popup (just like radalert) is created and handled on the client. The only difference is that with radalert, you need to wait for the control (RadWindowManager) to load - note that in ASP.NET AJAX, all ASP.NET AJAX controls, including RadControls are created after the page has loaded.
    If you believe that there is an easier way to achieve this, please open a support ticket and send us a full, working solution where you have implemented your logic with a standard browser's alert (window.alert()) - we will examine it right away.

  • Telerik Admin , Oct 17, 2008

    Hi jo,
    Have you tried Ricardo's solution? I would only recommend not to add this code in the ResponseScripts collection but to use the RegisterStartupScript method of the ScriptManager. The reason for this is that when add a script in ResponseScripts, they are not executed - they will be next time the RadAjaxManager is used while the ASP.NET AJAX ScriptManager can fire them immediately.

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.