它是一个很好的做法写HTML在我的ASP.NET codebehind使用一个StringBuilder? [英] Is it a Good Practice to Write HTML Using a StringBuilder in my ASP.NET Codebehind?

查看:114
本文介绍了它是一个很好的做法写HTML在我的ASP.NET codebehind使用一个StringBuilder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣从其他开发商,听取他们对一种方法,我通常采取的意见。我有一个Web应用程序,asp.net 2.0,C#。

我最常做的写出来的下拉菜单,表格,输入控件等在后面使用的StringBuilder的code和写出来的东西像sb.Append(

我不使用许多.NET控件我通常写出来的code中的HTML后面找到自己。当我想使用jQuery或调用JavaScript我只是把该函数调用像sb.Append(TD我sb.Append标签...的onblur ='fnCallJS()'。

我已经得到了pretty舒适的使用这种方法。对于数据访问我使用EntitySpaces。

我只是一种好奇,如果这种做法是可怕的错误,确定根据上下文,好,时间去学习3.0,等我感兴趣的学习只是在寻找一些投入。


修改

在这里阅读评论后这听起来像我应该看看MVC。我没有做到这一点呢。这样做的唯一犹豫的是,现有的项目就是这样,现有的。有很多code的已经做了我解释的方式,它是很难想象会有什么改变它参与,这样做,只是学什么,将采取优势。

我是从评论带走的另一件事是,我的code后面应该真的不包括许多sb.Append code的,而现在则是在众多功能充满了它。对我来说,是不是混乱,但就是因为我知道每个功能是否和自己能看什么它看,哦,那写出的x,y和z。

这并不罕见,我只是对部分的.aspx一个div,然后建立的,该.innerHtml与在code后面的StringBuilder。

您的意见再次感谢。我想因为我阅读。


解决方案

  

我通常写出来的code后面的HTML。


这部分是一个有点古怪,而不是我推荐的WebForms。如果你想做到这一点,考虑一个asp.net MVC项目来代替。

在网络表单,你真的想你的HTML的肉一起生活的标记,而不是code。这两个应该保持独立。你也不想,它包括你的整个页面一个巨大的StringBuilder。这将迫使你保持整个页面在内存中的两次的(一次用于StringBuilder的字节,一次在年底内置字符串),而不是它的建成写的页面响应流。这意味着每个请求更多的内存,它可以真的杀了可扩展性。

为了达到这些目标,我就要你的StringBuilder code到,你可以在ASPX标记使用自定义/用户控件的抽象的不同部分。这些控件可以使用StringBuilder来创建它们的输出。这意味着你只需要保持足够的HTML标记在内存来渲染一次一个控制。它也可以让你更轻松地跨页,甚至网站上重复使用常用的标记。

I'm interested to hear from other developers their opinion on an approach that I typically take. I have a web application, asp.net 2.0, c#.

What I usually do to write out drop downs, tables, input controls, etc. is in the code behind use StringBuilder and write out something like sb.Append("

I don't find myself using to many .net controls as I typically write out the html in the code behind. When I want to use jQuery or call JavaScript I just put that function call in my sb.Append tag like sb.Append("td...onblur='fnCallJS()'.

I've gotten pretty comfortable with this approach. For data access I use EntitySpaces.

I'm just kind of curious if this sort of approach is horribly wrong, ok depending on the context, good, time to learn 3.0, etc. I'm interested in learning and was just looking for some input.


Edit

After reading the comments here it sounds like I should take a look at MVC. I've not done that yet. The only hesitancy in doing so is that the existing project is just that, existing. There is a lot of code already done the way I explained and it is hard to imagine what would be involved in changing it, advantages of doing so, and just learning what that would take.

The other thing I'm taking away from the comments is that my code behind should really not include much of the sb.Append code, whereas now it is filled with it in numerous functions. To me it is not messy but that is because I know what each function does and can look at it and see, oh that writes out x, y, and z.

It's not uncommon for me to just have a div on the .aspx part and then build up the .innerHtml of that with the StringBuilder in the code behind.

Thanks again for the comments. I'm thinking as I'm reading them.

解决方案

I typically write out the html in the code behind.

That part is a little odd, and not something I recommend for webforms. If you want to do that, consider an asp.net mvc project instead.

In webforms, you really want the meat of your html to live with the markup rather than the code. The two should remain separate. You also don't want a huge stringbuilder that encompasses your entire page. This will force you to keep the entire page in memory twice (once for the stringbuilder bytes and once for the built string at the end) rather than writing the page to the response stream as it's built. That means more memory per request, which can really kill scalability.

To those ends, I would abstract distinct portions of your stringbuilder code into custom/user controls that you can use in the aspx markup. These controls can use a stringbuilder to create their output. This means you only need to keep enough html markup in memory to render one control at a time. It also allows you to more easily re-use common markup across pages or even sites.

这篇关于它是一个很好的做法写HTML在我的ASP.NET codebehind使用一个StringBuilder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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