7

I'm wondering if anyone has any experience converting User controls to Web controls?

Ideally, I'd like to offload some of the design work to others, who would give me nicely laid out User Controls. Then, I could go through the process of converting, compiling, testing and deploying.

Until MS comes up with the magic "Convert to Server Control" option, it looks like I'm pretty well stuck with re-writing from scratch. Any ideas?

Chris Cudmore
  • 28,156
  • 12
  • 52
  • 92
  • What would be wrong with using the User controls ? If you need to share logic, you could have the .ascx files inherit from a base class which you define in a class library, so that the logic can be easily reused; while maintaining a flexible visual design that can be easily changed. – driis Jun 01 '09 at 16:05
  • I searched for hours and tried a lot. The only thing worked for me was this article https://blogs.msdn.microsoft.com/davidebb/2005/10/31/turning-an-ascx-user-control-into-a-redistributable-custom-control/. It says `self-contained` with given restrictions. I used a `WebSite` project and had to inline the code behind into the ascx file. So basically i just have a single file left for the user control. – djmj Aug 22 '16 at 15:27

3 Answers3

6

Is there a reason you must convert these user controls to server controls? Remember that it is possible to compile a user control into an assembly.

Andrew Hare
  • 320,708
  • 66
  • 621
  • 623
  • 1
    Web archive (due to offline website): http://web.archive.org/web/20120328081058/http://www.nathanblevins.com/2008/06/compile-a-web-user-control-into-a-dll-net-c/ – Caramiriel Feb 24 '15 at 07:12
1

You are right there is no magic bullet here but since you already have a User Control its not that difficult.

  1. Make sure all properties, events, etc. are set in the code behind since you won't have any mark up when you're done
  2. Create a new Server Control
  3. Paste all of the event handling and property setting code into the new control
  4. override the Render method for each child control call the RenderControl Method passing in the supplied HtmlTextWriter

    protected override void Render(HtmlTextWriter writer)
    {
        TextBox box = new TextBox();
        //Set all the properties here
        box.RenderControl(writer);
        base.Render(writer);
    }
    
Jeremy
  • 6,312
  • 2
  • 22
  • 32
0

I searched for hours and found many blogs about it.

The only thing worked for me was this article https://blogs.msdn.microsoft.com/davidebb/2005/10/31/turning-an-ascx-user-control-into-a-redistributable-custom-control/.

It says self-contained with given restrictions, but it does not mentions that the codebehind must be included in ascx file.

I used a Web Site project (not Web application!) and had to inline the code behind into the ascx file and only use control directive like:

<%@ Control Language="C#" ClassName="MyPackage.MyControl"%>

So basically i just have a single file left for the user control. When codebehind was a separate file all control's where null when i referenced the final dll.

I also tried http://blog.janjonas.net/2012-04-06/asp_net-howto-user-control-library-compile-dll-file but with reflection the ascx file could not be found.

djmj
  • 5,266
  • 4
  • 47
  • 89