Carls Jr.
Carls Jr.

Reputation: 3078

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.

Upvotes: 2

Views: 2341

Answers (2)

M4N
M4N

Reputation: 96561

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);

Upvotes: 1

Luc
Luc

Reputation: 1528

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".

Upvotes: 0

Related Questions