Welcome to MSDN Blogs Sign in | Join | Help

News

  • These postings are provided "AS IS" with no warranties, and confer no rights. Use of included code samples are subject to the terms specified Terms of Use
Tip #87: Did you know… How to reuse a web page by converting into an ASP.NET User Control?

If you want to reuse a web form's content in other places in your application, you can easily convert the page into an ASP.NET User Control. Then you can add that user control to any other page.

For example, you have a page in your application that looks like the following:

WebForm

 

Now, you need to rename to the file to have a .ascx extension. Then remove html, head, body, form tags from the page. Change the "@ Page" directive to a "@ Control" directive.  Remove all attributes of the "@ Control" directive, except Language, AutoEventWireup, CodeFile, and Inherits. You also need the change the Inherits to have your new UserControl code behind file's name, like in the image below. Close and re-open the file to make squiggles go away.

MyUserControl 

 

If you have a code-behind file, its name will be changed properly for you automatically. Open the file and change the class it inherits from to UserControl, and the class name to match your .ascx file. See below.

MyUserControlCodeBehind

More information can be found here.

Posted: Thursday, July 09, 2009 6:10 PM by WebDevTools

Comments

Bjorn van der Neut said:

Funny this is also an .NET exame question :-)

# July 10, 2009 2:21 AM

dahlbyk said:

You would also want to remove the DOCTYPE.

# July 10, 2009 4:25 PM

L said:

It would be easier to create the user control and copy the content into it.

# July 16, 2009 4:20 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker