从客户端加载usercontrol

时间:2014-01-01 10:39:52

标签: asp.net extjs ext.net

我正在加载我的用户控件,如下所示。我如何从客户端获取该文件。

相同的代码,但客户端请求我需要的

private void LoadUserControlTab(string num, string title = "")
{

    Ext.Net.Panel pn = new Ext.Net.Panel();
    pn.Title = title;
    pn.ID = num;
    string pnid = num;
    pn.Closable = true;
    pn.Flex = 1;
    pn.Height = 500;
    currentUC = (UserControl)this.LoadControl(string.Format("Controls/UserControl{0}.ascx", num));
    currentUC.ID = "UC" + num;
    pn.ContentControls.Add(currentUC);
    pn.AddTo(this.anaTabPnl);
    this.anaTabPnl.SetActiveTab(pnid);
    this.Controls.Add(pn);
        //  this.Panel1.ContentControls.Add(currentUC);
        //  this.anaTabPnl.ContentControls.Add(currentUC);
}

这是我在加载外部页面时常用的客户端

<ext:XScript ID="XScript1" runat="server">
    <script>
        var addTab = function (tabPanel, id, url, menuItem,mytitle) {
            var tab = tabPanel.getComponent(id);

            if (!tab) {
                tab = tabPanel.add({
                    id       : id,
                    title    : mytitle,
                    closable : true,
                    flex:"1",
                    menuItem : menuItem,
                    loader   : {
                        url      : url,
                        renderer : "frame",
                        loadMask : {
                            showMask : true,
                            msg      : "Yükleniyor"
                        }
                    }
                });

                tab.on("activate", function (tab) {
                    #{MenuPanel1}.setSelection(tab.menuItem);
                });
            }

            tabPanel.setActiveTab(tab);
        }
    </script>
</ext:XScript>

我应该只指向url,ascx ??

3 个答案:

答案 0 :(得分:1)

创建一个Web服务并公开一个webmethod

[WebMethod]
public string LoadUserControl(int num)
{
    using (Page page = new Page())
    {
        UserControl userControl = (UserControl)page.LoadControl(string.Format("Controls/UserControl{0}.ascx", num));

        page.Controls.Add(userControl);
        using (StringWriter writer = new StringWriter())
        {
            page.Controls.Add(userControl);
            HttpContext.Current.Server.Execute(page, writer, false);
            return writer.ToString();
        }
    }
}

之后从javascript(jquery)调用此Web服务方法,如下所示

 $("#load").live("click", function () {
            $.ajax({
                type: "POST",
                url: "WebService/LoadUserControl",
                data: "{num:[VALUE]}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (r) {
                    $("#Content").append(r.d);
                }
            });
        });

答案 1 :(得分:0)

这是ashx实现;

myHanndler.ashx

public void ProcessRequest(HttpContext context)
    {
        string mainTabPnl = context.Request["container"];
        string url = context.Request["url"];
        string id = context.Request["id"];

        Ext.Net.Panel pn = new Ext.Net.Panel()
        {
            Title = "MY TITLE",
            Closable = true,
            Layout = "Fit",
            Height = 500,
            ID = id,

            Items = {
                new UserControlLoader{
                Path=url
                }
            }
        };

        pn.AddTo(mainTabPnl);

        new DirectResponse().Return();
    } 

和客户端

<ext:XScript ID="XScript1" runat="server">

    <script>

        var onClickMeLoadComponent = function (tabPanel, id, url, menuItem) {

            var tab = #{mainTabPnl}.getComponent(id);

            if (!tab) {

                Ext.net.DirectMethod.request({
                    url: "MyHandler.ashx",
                    cleanRequest: true,
                    params: {
                        container: tabPanel,
                        url: url,
                        id:id
                    }, complete: function () {
                        var tab = #{mainTabPnl}.getComponent(id);
                       #{mainTabPnl}.setActiveTab(tab);
                    }
                });

                //tab.on("activate", function (tab) {
                //   #{mainTabPnl}.setSelection(tab.menuItem);
                //});
            }
           #{mainTabPnl}.setActiveTab(tab);
        };
            </script>
    </ext:XScript>

答案 2 :(得分:0)

对于ExtJ,您可以使用以下代码。

这是ashx的实现。

public void ProcessRequest(HttpContext context)
    {
        string mainTabPnl = context.Request["container"];
        string url = context.Request["controlUrl"];

        new DirectResponse(this.createNew(url).ToScript(RenderMode.AddTo, context.Request["container"])).Return();

    }

    private Panel createNew(string url)
    {
        Ext.Net.Panel pnlBase = new Ext.Net.Panel()
        {
            Title = "MY TITLE",
            Closable = false,
            Layout = "Fit",
            Height = 500,
            ID = "pnlBase",

            Items = {
            new UserControlLoader{
            Path=url
            }
        }

       };

      return pnlBase;
    }

这是extjs代码。

win = new Ext.Window({
        id: 'win',
        title: 'Add New',
        layout: 'fit',
        autoScroll: true,
        y: 120,
        width: 900,
        height: 700,
        modal: true,
        closeAction: 'hide',
        listeners: {
            activate: {
                fn: function () {
                    Ext.net.DirectEvent.request({
                        url: "MyHandler.ashx",
                        cleanRequest: true,
                        extraParams: {
                            container: this.id,
                            controlUrl: 'MyUserControl.ascx'
                        },
                        eventMask: {
                            showMask: true
                        }
                    });
                },
                single: true
            }
        }

    });

    win.show();