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

Problem with radalert

2 Answers 126 Views
Window
This is a migrated thread and some comments may be shown as answers.
Richard Weeks
Top achievements
Rank 2
Richard Weeks asked on 04 Oct 2010, 07:09 AM
I'm trying to build an alert into my master page that can be accessed from anywhere in the UI code-behind. Any method, any function.

I was doing quite well (I think) but I'm stuck now.

Master page

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Default.Master.cs" Inherits="Web.App_Templates.Default.Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head id="Head" runat="server">
    <title><%= Page.Title %></title>
</head>
<body>
  
    <form id="MainForm" runat="server">
  
        <telerik:RadScriptManager 
            ID="ScriptManager" 
            runat="server">
        </telerik:RadScriptManager>
          
        <telerik:RadAjaxManager 
            ID="TelerikAjaxManager" 
            runat="server">
        </telerik:RadAjaxManager>
          
        <telerik:RadWindowManager 
            ID="TelerikWindowManager" 
            runat="server">
            <Windows>
                <telerik:RadWindow 
                    ID="TelerikWindow" 
                    runat="server">
                </telerik:RadWindow>
            </Windows>
        </telerik:RadWindowManager>
  
        <asp:UpdatePanel 
            ID="UpdatePanel" 
            runat="server">
            <ContentTemplate>
                
                <asp:Literal 
                    ID="Feedback" 
                    runat="server" 
                    Text=" " />
  
                <asp:ContentPlaceHolder 
                    ID="MainContentPlaceHolder" 
                    runat="server">
                </asp:ContentPlaceHolder>
                      
            </ContentTemplate>
        </asp:UpdatePanel>
  
    </form>
  
</body>
</html>

Master page code behind

namespace Web.App_Templates.Default
{
    using System;
  
    public partial class Default : System.Web.UI.MasterPage
    {
        public void ShowFeedback(string feedBackText, bool useAlert)
        {
            if (useAlert)
            {
                string radalertscript = string.Format("<script language='javascript'>function f(){{radalert('{0}', 330, 210);Sys.Application.remove_load(f);}};Sys.Application.add_load(f);</script>", feedBackText);
                  
                Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", radalertscript); 
            }
            else
            {
                this.Feedback.Text = feedBackText;                
            }
        }
    }
}

Web content page

<%@ Page Title="Welcome" Language="C#" MasterPageFile="~/App_Templates/Default/Default.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Web.Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ MasterType VirtualPath="~/App_Templates/Default/Default.Master" %>
  
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
  
    <asp:Button 
        ID="TestFeedbackAlert" 
        runat="server" 
        Text="Click Me" 
        OnClick="TestFeedbackAlert_Click" 
        Width="100" />
     
    <asp:Button 
        ID="TestFeedbackNoAlert" 
        runat="server" 
        Text="Click Me" 
        OnClick="TestFeedbackNoAlert_Click" 
        Width="100" />
  
</asp:Content>

And the code behind

namespace Web
{
    using System;
    using System.Web.UI;
  
    public partial class Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //this.Master.ShowFeedback("<p>Hello, AppWorld!</p>", true);
            }
        }
  
        protected void TestFeedbackAlert_Click(object sender, EventArgs e)
        {
            this.Master.ShowFeedback("<p>Clicked!</p>", true);
        }
  
        protected void TestFeedbackNoAlert_Click(object sender, EventArgs e)
        {
            this.Master.ShowFeedback("<p>Clicked!</p>", false);
        }
    }
}

The alert will show if you uncomment out the entry in the page load.  It will also work if you click the TestFeedbackNoAlert button.

It will not work if you click the TestFeedbackAlert button.

I've looked at the suggestions at:
http://www.telerik.com/support/kb/aspnet-ajax/window/calling-radalert-from-codebehind.aspx

But I'm none the wiser. Can anyone help? I need it to work no matter where it gets called (page load, postback, ajax call, etc.)

Regards,
Richard



2 Answers, 1 is accepted

Sort by
0
Georgi Tunev
Telerik team
answered on 04 Oct 2010, 01:13 PM
Hello Richard,

This problem is not related to the RadWindow control - with your current logic, even if you call a simple alert, instead of radalert, you will get the same behavior.

You need to change the logic a bit and use ScriptManager instead, i.e.:
public void ShowFeedback(string feedBackText, bool useAlert)
    {
        if (useAlert)
        {
            string radalertscript = string.Format("function f(){{radalert('{0}', 330, 210);Sys.Application.remove_load(f);}};Sys.Application.add_load(f);", feedBackText);
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "test", radalertscript, true);
 
        }
        else
        {
            this.Feedback.Text = feedBackText;
        }
    }

Important: I see that you named the RadScriptManager "ScriptManager" - this is not a good practice, as you will not be able to reference the ScriptManager from codebehind. Change the Id of the control to something else - "ScriptManager1" for example.

Greetings,
Georgi Tunev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Richard Weeks
Top achievements
Rank 2
answered on 05 Oct 2010, 12:14 AM
Georgi,

Thanks for your answer, I'd already arrived at your solution but received a confusing error message because I'd missed the typo on naming the script manager as you quite rightly pointed out.

Once I changed to use my normal naming convention (I put Telerik in front of my Rad control IDs, so TelerikScriptManager in this case), everything started working.

I now have:

public void ShowFeedback(string feedBackText, bool useAlert)
{
    if (useAlert)
    {
        string radAlertScript = string.Format("function f(){{radalert('{0}', 330, 210);Sys.Application.remove_load(f);}};Sys.Application.add_load(f);", feedBackText);
  
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), "test", radAlertScript, true);
    }
    else
    {
        this.Feedback.Text = feedBackText;                
    }
}

Called easily as:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.Master.ShowFeedback("Hello, AppWorld!", true);
    }
}

Just need to remember the following in the web content page:

<%@ MasterType VirtualPath="~/App_Templates/Default/Default.Master" %>

I hope others will find this useful.

Thanks,
Richard
Tags
Window
Asked by
Richard Weeks
Top achievements
Rank 2
Answers by
Georgi Tunev
Telerik team
Richard Weeks
Top achievements
Rank 2
Share this question
or