【问题标题】:How to display Telerik RadWindow from code-behind of UserControl如何从 UserControl 的代码隐藏中显示 Telerik RadWindow
【发布时间】:2016-08-09 07:32:11
【问题描述】:

以下是我用来在 aspx 页面中显示 Telerik(Radwindow) 弹出窗口的代码。它成功地显示了带有以下当前代码的窗口。

如何从 ASP.NET 用户控件显示弹出窗口?

RadWindowManager windowManager = new RadWindowManager();
RadWindow window1 = new RadWindow();

window1.NavigateUrl = "Window1.aspx";
window1.ID = "RadWindow1";
window1.VisibleOnPageLoad = true; // Set this property to True for showing window from code   
windowManager.Windows.Add(window1);
this.form1.Controls.Add(window1);

【问题讨论】:

    标签: c# asp.net telerik


    【解决方案1】:

    当然可以,但是这种方法有两个问题:

    • 用户控件必须知道母版页上的窗口管理器和窗口,遍历控件层次结构并找到它们

    • 如果您将整个 sn-p 添加到用户控件,您最终会得到多个窗口管理器实例,这可能会对您起到一些作用(请参阅 here)。

    所以,考虑以下想法:

    • 将 RadWindow 实例添加到用户控件(不是 RadWindowManager)并单独使用它。阅读this article 以从服务器注册一个脚本以便打开它,阅读this article 以使每个用户控制可能需要唯一的 JS 函数。

    • 完全从客户端打开 RadWindow,如 here 所示。您可以register a JS function from the server 传递您需要的参数(URL、模式等)

    以下是根据您的评论对其中一个想法(我会采用)的示例实现:

    母版页

            <telerik:RadWindowManager ID="RadWindowManager1" runat="server"></telerik:RadWindowManager>
            <script>
                function openDialog(url, modal, width, height) {
                    if (radopen) { //if not, there is no RadWindowManager on the page, add an else{} block to use window.open() or other logic
                        var wnd = radopen(url, null);
                        wnd.set_destroyOnClose(true);
                        //add checks here in case parameters have not been passed
                        wnd.setSize(width, height);
                        wnd.center();
                        wnd.set_modal(modal);
                    }
                }
            </script>
    

    用户控件标记

    <asp:Button ID="Button1" Text="open RW" OnClick="Button1_Click" runat="server" />
    

    使用控制代码隐藏

    protected void Button1_Click(object sender, EventArgs e)
    {
        bool flag = true;
        if(flag)
        {
            string script = string.Format("function f(){{openDialog('{0}', {1}, {2}, {3});Sys.Application.remove_load(f);}}Sys.Application.add_load(f);",
                                         "the-page.aspx",
                                         "true",
                                         600,
                                         400);
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "someKey", script, true);
        }
    }
    

    【讨论】:

    • 是的。你说的对。我有用户控件和母版页。我只需要从服务器端显示 RadWindow。单击保存按钮后,会完成一些计算,如果不满足保存条件,我需要动态显示 Telerik RadWindow。我同意你的看法。你能给我一个更简单的解决方案吗?
    • 我添加了一个示例可重用实现示例,在这种情况下我会这样做。当然,重构和调整一下,这只是一个示例
    • 太棒了。但为什么我要触摸母版页?可以在托管用户控件的 aspx 页面中完成吗?热切期待您的回复。
    • 当然可以,但我只是将处理打开通用对话框的常用函数放在一起,因为它可以在任何地方重复使用。正如我所说,根据您的设置进行重构。
    猜你喜欢
    • 1970-01-01
    • 2012-06-25
    • 2012-01-29
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    相关资源
    最近更新 更多