javascript 使用 RadButton 关闭 RadWindow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8848628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:46:04  来源:igfitidea点击:

Closing RadWindow using RadButton

javascriptasp.nettelerikradwindow

提问by Peter Pocket

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:

我正在尝试使用 RadButton 关闭窗口本身的 radwindow(通过 javascript)。是否可以调用脚本来关闭窗口?这是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.

我已经尝试了所有方法,该脚本仅在我使用纯 HTML 元素(例如锚标记)时才有效。如果我使用 OnClick 事件,当窗口打开时会出现以下错误:Compiler Error Message: CS1026: ) expected.

Am I missing something?

我错过了什么吗?

Thanks!

谢谢!

回答by Acadia

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:

我不确定我是否在改进这个答案,我只是想让它更容易理解。我有一个从主页打开的 rad 窗口。radwindow 在代码隐藏 (C#) 中打开,而不是 Javascript。当我的用户单击 RadWindow 上的 Save 按钮时,它会执行一些逻辑任务,然后关闭 radwindow 本身。您只需要:

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

把这个代码块放在你的 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)

执行预关闭逻辑后,将此代码放在 RadWindow 的按钮单击中(执行其他逻辑的同一按钮关闭窗口)

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

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

OR

或者

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

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

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

如果您想知道如何从代码隐藏中打开 radwindow,我是这样做的:

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:

...当然,您需要在打开窗口的主页上使用基本的 RadWindowManager:

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

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

如果我犯了错误,这应该有效,请纠正我。

Thanks

谢谢

回答by rdmptn

The way to call a function from the RadButton is by using either its OnClientClicked or OnClientClicking event. Then you need to pass only the name of the JavaScript function, without parenthese. OnClick is a property for the server handler, this is also the case with the regular asp button. Try this:

从 RadButton 调用函数的方法是使用其 OnClientClicked 或 OnClientClicking 事件。然后你只需要传递 JavaScript 函数的名称,不带括号。OnClick 是服务器处理程序的属性,常规 asp 按钮也是如此。试试这个:

<telerik:RadButton ID="CancelButton" runat="server" OnClientClicked="closeWindow" AutoPostBack="false" CssClass="clicker" Text="Cancel" UseSubmitBehavior="False" EnableEmbeddedScripts="false" CausesValidation="False" RegisterWithScriptManager="False">

Note the AutoPostBack property is set to false to prevent the postback.

请注意 AutoPostBack 属性设置为 false 以防止回发。