通过从服务器调用 javascript 关闭 RadWindow
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7757558/
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
Closing RadWindow by invoking javascript from server
提问by kmkemp
I have a Control that looks like this:
我有一个看起来像这样的控件:
<telerik:RadCodeBlock runat="server">
<script type="text/javascript">
function refresh() {
window.$find("<%= RadAjaxManager1.ClientID %>").ajaxRequest("RebindRecommendations");
}
</script>
</telerik:RadCodeBlock>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="AjaxRequest">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadAjaxManager1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="NameOfGrid" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
<Windows>
<telerik:RadWindow ID="RadWindow1" runat="server" NavigateUrl="UrlOfPage" OnClientClose="refresh"></telerik:RadWindow>
</Windows>
</telerik:RadWindowManager>
Page:
页:
<script type="text/javascript">
function closeWindow() {
self.close();
return false;
}
</script>
<!-- form fields here -->
<telerik:RadButton runat="server" ID="_cancel" Text="Cancel" OnClientClick="closeWindow"></telerik:RadButton>
<telerik:RadButton runat="server" ID="_submit" Text="Submit" OnClientClick="closeWindow" OnClick="DoSomeDataBaseStuff"></telerik:RadButton>
This works as intended. My RadWindow is removed and my control's grid refreshes upon hitting either the cancel or submit buttons. The problem is that the database isn't finished doing its work by that time so the grid refresh doesn't reveal the changes. I have attempted to switch my implementation of page to look like this:
这按预期工作。我的 RadWindow 被删除,我的控件网格在点击取消或提交按钮时刷新。问题是到那时数据库还没有完成它的工作,所以网格刷新不会显示更改。我试图将我的页面实现切换为如下所示:
Page:
页:
<script type="text/javascript">
function closeWindow() {
self.close();
return false;
}
</script>
<!-- form fields here -->
<telerik:RadButton runat="server" ID="_cancel" Text="Cancel" OnClick="CallJavaScriptToKillWindow"></telerik:RadButton>
<telerik:RadButton runat="server" ID="_submit" Text="Submit" OnClick="DoSomeDataBaseStuffAndThenCallJavaScriptToKillWindow"></telerik:RadButton>
Code Behind eventually calls this after database work completes:
数据库工作完成后,代码隐藏最终会调用它:
ClientScript.RegisterStartupScript(GetType(), "Key", "closeWindow();", true);
I hit my breakpoint in the closeWindow function, but it doesn't have the same behavior (the window doesn't actually close). I've tried various iterations like:
我在 closeWindow 函数中遇到了断点,但它没有相同的行为(窗口实际上并未关闭)。我尝试了各种迭代,例如:
ClientScript.RegisterStartupScript(GetType(), "Key", "$(document).ready(function() {return closeWindow();});", true);
to no avail. What am I missing?
无济于事。我错过了什么?
回答by chrisdrobison
What is 'self'? Don't you mean 'this'?
什么是“自我”?你不是说'这个'吗?
Are you trying to close the RadWindow from inside the window or from the page that launches the window?
您是尝试从窗口内部还是从启动窗口的页面关闭 RadWindow?
From inside a window, I usually do this:
从窗口内部,我通常这样做:
function GetRadWindow()
{
var oWindow = null;
if (window.radWindow)
oWindow = window.radWindow;
else if (window.frameElement.radWindow)
oWindow = window.frameElement.radWindow;
return oWindow;
}
function Close()
{
var oWindow = GetRadWindow();
oWindow.argument = null;
oWindow.close();
return false;
}
Also, I'd use ScriptManager.RegisterStartupScript rather than ClientScript.
另外,我会使用 ScriptManager.RegisterStartupScript 而不是 ClientScript。
回答by James Johnson
I'm pretty sure that I've run into this issue before, and I think this fixed it:
我很确定我以前遇到过这个问题,我认为这解决了它:
setTimeout("self.close()", 1000);
If that doesn't work, try this:
如果这不起作用,请尝试以下操作:
setTimeout("self.focus()", 500); //adjust the timeout as needed
setTimeout("self.close()", 1000);
You can also do it this way if you'd prefer:
如果您愿意,也可以这样做:
setTimeout(function(){
self.close();
}, 1000);
回答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
谢谢