Closing RadWindow using RadButton

Peter Scott picture Peter Scott · Jan 13, 2012 · Viewed 15k times · Source

I'm trying to use a RadButton to close a radwindow from with the window itself (via javascript). Is it possible to call a script to close the window? Here is the javascript:

function getRadWindow() 
{
  var oWindow = null;
  if (window.radWindow) oWindow = window.radWindow;
  else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
  return oWindow;
}

function closeWindow() 
{
  getRadWindow().close();
}

And here is the button:

<telerik:RadButton ID="CancelButton" runat="server" OnClick="closeWindow();" CssClass="clicker" Text="Cancel" UseSubmitBehavior="False" EnableEmbeddedScripts="false" CausesValidation="False" RegisterWithScriptManager="False">
</telerik:RadButton>

I have tried everything, the script will only work if I use a pure HTML element such as an anchor tag. If I use the OnClick event I get the following error when the window opens: Compiler Error Message: CS1026: ) expected.

Am I missing something?

Thanks!

Answer

Acadia picture Acadia · Apr 10, 2012

I'm not sure if I am improving this answer, I'm just trying to make it easier to understand. I have a rad window that is opened from a main page. The radwindow is opened in Code Behind (C#), not Javascript. When my user clicks a Save button on the RadWindow, it performs some logic tasks, then it closes the radwindow itself. You simply need to:

Put thise code block in you RadWindow aspx.....

<telerik:RadCodeBlock runat="server" ID="rcb1">
<script language="javascript" type="text/javascript">

function GetRadWindow() 
{
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function CloseDialog(button) 
{
GetRadWindow().close();
}  

</script>
</telerik:RadCodeBlock>

Put this code in your RadWindow's button click after you perform your pre-close logic (the same button that performs the other logic closes the window)

C# ClientScript.RegisterStartupScript(typeof(string), "", "CloseDialog();");

OR

VB ClientScript.RegisterStartupScript(Me.GetType(), "", "CloseDialog();")

If you're wondering how to open the radwindow from codebehind here is how I did it:

RadWindow window1 = new RadWindow();
// Set the window properties   
window1.NavigateUrl = "winStrengthChart.aspx?EMPLOYIDNAME=" + parmString;
window1.ID = "RadWindow1";
window1.Width = 800;
window1.Height = 650;
window1.VisibleStatusbar = false;
window1.Behaviors = Telerik.Web.UI.WindowBehaviors.Close | Telerik.Web.UI.WindowBehaviors.Resize | Telerik.Web.UI.WindowBehaviors.Move;
window1.VisibleOnPageLoad = true; // Set this property to True for showing window from code   
rwm1.Windows.Add(window1);
this.Form1.Controls.Add(window1);   

...AND of course you need a basic RadWindowManager on the main page that opens the window:

<telerik:RadWindowManager ID="rwm1" runat="server">
<Windows>
</Windows>
</telerik:RadWindowManager>

This should work if I have made a mistake please correct me.

Thanks