将用户控件转换为服务器控件

时间:2021-08-14 15:59:34

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

我想知道是否有人有将用户控件转换为Web控件的经验?

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?

直到MS提出神奇的“转换为服务器控制”选项,看起来我很难从头开始重写。有任何想法吗?

3 个解决方案

#1


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.

您是否有必要将这些用户控件转换为服务器控件?请记住,可以将用户控件编译到程序集中。

#2


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. 确保在后面的代码中设置了所有属性,事件等,因为完成后您将没有任何标记

  3. Create a new Server Control
  4. 创建一个新的服务器控件

  5. Paste all of the event handling and property setting code into the new control
  6. 将所有事件处理和属性设置代码粘贴到新控件中

  7. override the Render method for each child control call the RenderControl Method passing in the supplied HtmlTextWriter

    覆盖每个子控件的Render方法调用RenderControl方法传递提供的HtmlTextWriter

    protected override void Render(HtmlTextWriter writer)
    {
        TextBox box = new TextBox();
        //Set all the properties here
        box.RenderControl(writer);
        base.Render(writer);
    }
    

#3


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

对我来说唯一有用的是这篇文章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.

它说具有给定限制的自包含,但它没有提到代码隐藏必须包含在ascx文件中。

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:

我使用了一个Web站点项目(而不是Web应用程序!),并且必须将后面的代码内联到ascx文件中,并且只使用控制指令,如:

<%@ 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.

所以基本上我只有一个文件留给用户控件。当codebehind是一个单独的文件时,当我引用最终的dll时,所有控件都为null。

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.

我也尝试了http://blog.janjonas.net/2012-04-06/asp_net-howto-user-control-library-compile-dll-file,但是通过反射无法找到ascx文件。

#1


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.

您是否有必要将这些用户控件转换为服务器控件?请记住,可以将用户控件编译到程序集中。

#2


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. 确保在后面的代码中设置了所有属性,事件等,因为完成后您将没有任何标记

  3. Create a new Server Control
  4. 创建一个新的服务器控件

  5. Paste all of the event handling and property setting code into the new control
  6. 将所有事件处理和属性设置代码粘贴到新控件中

  7. override the Render method for each child control call the RenderControl Method passing in the supplied HtmlTextWriter

    覆盖每个子控件的Render方法调用RenderControl方法传递提供的HtmlTextWriter

    protected override void Render(HtmlTextWriter writer)
    {
        TextBox box = new TextBox();
        //Set all the properties here
        box.RenderControl(writer);
        base.Render(writer);
    }
    

#3


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

对我来说唯一有用的是这篇文章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.

它说具有给定限制的自包含,但它没有提到代码隐藏必须包含在ascx文件中。

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:

我使用了一个Web站点项目(而不是Web应用程序!),并且必须将后面的代码内联到ascx文件中,并且只使用控制指令,如:

<%@ 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.

所以基本上我只有一个文件留给用户控件。当codebehind是一个单独的文件时,当我引用最终的dll时,所有控件都为null。

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.

我也尝试了http://blog.janjonas.net/2012-04-06/asp_net-howto-user-control-library-compile-dll-file,但是通过反射无法找到ascx文件。