ASP.NET 自定义控件样式

发布于 2024-07-05 10:05:06 字数 773 浏览 8 评论 0原文

我正在开始研究几个 ASP.NET 自定义控件。 我想知道我是否可以得到一些关于你们的男孩/女孩关于如何将样式应用到控件的想法的意见。

我宁愿把它推到 CSS 上,所以对于我过去做过的几个控件,我只是简单地粘贴了一个字符串属性,它允许您输入字符串,然后将其放入“样式 " 渲染时的属性。 我知道我还可以使用“CSSClass”属性并应用“class”属性。

我在创建“正确的”Style 属性(您实际上保存样式对象,并使用设计器指定其值)方面没有做太多事情。 对我来说,这似乎需要大量工作,而且说实话,我讨厌样式编辑器 UI,更愿意输入要应用的 CSS/类名称。

您对此有何看法?


注意:这是一种主观 - 因此需要明确:

接受的答案将是:

  • 提供各种方法的优点和缺点。
  • 欢迎提出意见,但好的答案应该是建设性的。
  • 用一些现实世界的知识/经验来支持它。

主观性没有。 人们很主观,没有思考、没有建设性或实际上提供一些见解和经验,这是一个问题

>>不要<<将其标记为“主观” - 该标记是浪费时间。 “主观”不是人们会寻找的技术或类别。 解决问题而不是忽略它。

I am in the process of beginning work on several ASP.NET custom controls. I was wondering if I could get some input on your guys/girls thoughts on how you apply styling to your controls.

I would rather push it so CSS, so for the few controls I have done in the past, I have simply stuck a string property which allows you so type in the string which in then slung in a "style" attribute when rendering. I know I could also use the "CSSClass" property and apply the "class" attribute.

I have not done much in the way of creating a "proper" Style property (in which you actually save the style object, and use the designer to specify its values). This to me seems like a lot of work, and TBH, I hate the Style editor UI and would much rather type in the CSS/class name to apply..

What are your thoughts on this?


Note: This is kind of subjective - so to be clear:

The accepted answer will be the one that:

  • Offers the pro's and con's of the various approaches.
  • Opinions are welcome, but a good answer should be constructive.
  • Backs it up with some real-world knowledge/experience.

There is nothing wrong with subjectivity. There is a problem with people being subjective and not thinking, being constructive or actually providing some insight and experience.

>>DO NOT<< tag this as "subjective" - that tag is a waste of time. "subjective" is not a technology or a category that people will look for. Fix the question rather than brush it off.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2024-07-12 10:05:06

这取决于自定义控件的使用方式 - 商业、可重新分发的控件应该与 VS IDE 兼容,并且在实现控件时按照用户期望的方式运行。

另一方面,如果您或您的团队是唯一使用该控件的人,那么浪费大量时间来使样式发挥作用是没有意义的,只要它的样式以合理的方式工作即可。

我实现的大多数自定义控件都使用属性来定义控件的外观和感觉,或者仅公开控件的成员自己的 CSSClass 属性。

争论归结为一致性与时间 - 任何元素都应该使用一致的样式机制,如果时间有限,则使用字符串方法,如果没有,则实现更复杂/IDE 友好的机制。

It would depend on how the custom controls are being used - A commercial, re-distributable control should be compliant with the VS IDE, and behave the way users expect it to when they implement the control.

On the other hand there is no point in wasting a lot of time to get styling to work if you or your team are the only ones to use the control, so long as it's styling works in a sane way.

Most of the custom controls I have implemented use a property to define the controls look and feel or just expose the controls' members own CSSClass properties.

The argument comes down to consistency vs. time - any element should use consistent styling mechanisms, if strapped for time, use a string method if not, implement a more complex / IDE friendly mechanism.

如果没有 2024-07-12 10:05:06

我认为您应该考虑自定义控件的“目标市场”,例如将使用它的人。

如果它是内部自定义控件,您几乎可以强制使用其中之一:如果它是公司内部的,您将有能力强制执行其一致性。

但是,如果它用于商业消费,则需要您提供一个选项来提供使用样式或类的方法。 典型的例子:ASP.NET 站点导航控件,例如,站点地图路径、菜单、树视图。 它们公开了一系列属性,以允许控件外观的各个方面使用样式、类或两者的组合。

I think you should consider your "target market" for the custom control, e.g., the people who will use it.

If it's an internal custom control, you can pretty much mandate the use of one or the other: if it's internal to the company you will have the ability to enforce its consistency.

If it's meant for commercial consumption, however, it is required that you give an option to provide a way to use either style or class. Case in point: the ASP.NET site navigation controls, e.g., SiteMapPath, Menu, Treeview. They have a bunch of properties exposed to allow either styles, classes, or a combination of both to each aspect of the controls' appearance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文