建立自定义为ASP.NET Web应用程序和Web窗体应用程序共同控制 [英] build custom control common for both ASP.NET web application and Web form application

查看:114
本文介绍了建立自定义为ASP.NET Web应用程序和Web窗体应用程序共同控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建只呈现状物体标记和一些JavaScript文件路径脚本自定义控件...
该控件将双方MVC和asp.net应用程序的网页表单

I have to create a custom control that just renders scripts like object tag and some javascript file path ... and this control is going to use by both MVC and asp.net webform application

请指教,如果这是可能的。

please advice if this is possible.

推荐答案

您最大的挑战是如何写HTML到屏幕上。 MVC的控制从System.Web.Mvc.ViewUserControl继承,而从的WebForms继承System.Web.UI.UserControl所以你必须有你的实际.ascx文件之间共享的类。

Your biggest challenge is how to write the HTML to the screen. MVC's controls inherit from System.Web.Mvc.ViewUserControl while WebForms inherits from System.Web.UI.UserControl so you'll have to have another class shared between your actual .ascx files.

编写与初乳哺乳&LT控制;%%>与HTML标记是怎么回事,因为这种继承不匹配很复杂混合

Writing the control with beestings <% %> mixed with HTML markup is going to be complicated because of this inheritance mismatch.

您最好的选择是操纵你的共享类的字符串来得到你想要的HTML,然后覆盖与最终结果的ToString()方法。这样,你可以调用的Response.Write(lControlClassThatDoesTheWork.ToString())在pageLoad的(或任何你需要的)事件在你的WebForms控制。对于MVC只需创建一个返回ControlClassThatDoesTheWorkand的一个实例的HtmlHelper的&lt;%=%> beesting将调用ToString()方法适合你。

Your best bet is to manipulate a string in your shared classes to get the HTML you want and then override the ToString() method with the final result. That way you can call Response.Write( lControlClassThatDoesTheWork.ToString() ) in the PageLoad ( or whatever you need ) event in your WebForms control. For MVC just create a HtmlHelper that returns an instance of ControlClassThatDoesTheWorkand the <%= %> beesting will call the ToString() method for you.

这篇关于建立自定义为ASP.NET Web应用程序和Web窗体应用程序共同控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆