简体   繁体   中英

how to load an aspx page into the webpart

I usually do this to load a web user control from the layouts directory.

protected override void CreateChildControls()
{
     try
     {

        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
           base.CreateChildControls();
           string strControlReference = "/_layouts/Controls/MyCustomControl.ascx";

           //instantiate the user control
           MyCustomControl ucControl = (MyCustomControl)Page.LoadControl(strControlReference);

           //add the control to webpart
           this.Controls.Add(ucControl);
        });
     }
     catch (Exception ex)
     {
        Common.WriteLogEvent(ex);
     }

}

Now what I wanted to do is, I have a web form "_/layouts/Page/MyPage.aspx" file under the layouts folder and I wanted to load this file instead of loading a typical ascx file on the webpart page.

Would it be possible? I would like to know how. Thank you.

You could add an <IFRAME> to the webpart and load the page into that.

Or create the IFRAME programmatically and add it in the same way you add the user controls:

var pageUrl = .....; // URL to your page
var literal = new Literal();
literal.Text = string.Format("<iframe src='{0}'></iframe>", pageUrl);
this.Controls.Add(literal);

You don't tell us, if "_/layouts/Page/MyPage.aspx" is under your control. If so, just create a new user control from it and then use it in the old page and in your web part.

There are some gotchas, like the user control does not have all the events the page does (you'll need to override appropriate methods), the variables on page are not directly accessible (just use user control's Page property), but if the page is not too complicated, the process should be just a repeat of "compile and change".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM