如何使 DropDownList 控件以粗体显示某些项目 ASP.NET
我正在 ASP.Net 中使用自定义 DropDownList 控件,并且请求使用 bold 字体显示列表中的某些项目(注意 - 该控件继承自 CompositeDataBoundControl,因此可以进行数据绑定...不是 DropDownListBox)。该控件绑定到一个表,并且表中有一个名为 IsUsed 的列 - 如果将其设置为 true,则列表中的相应项目应呈现为粗体。 (这里应该注意的是,这只能在 FireFox 中查看。)
我的经验都是在中间 \ 后端层,所以表示层对我来说非常新 - 有人可以给我指出正确的方向吗?我最初的想法是,在自定义控件中的某个位置,我可以访问从数据源返回的所有行,我可以循环访问这些行等,但我不确定这是否可能......还有我可以覆盖的 RenderContents ...看起来很有趣!
I'm working with a custom DropDownList control in ASP.Net and there's been a request to display certain items in the list with a bold typeface (NOTE - the control inherits from CompositeDataBoundControl so it can be data bound... not DropDownListBox). The control is bound to a table and there's a column in the table named IsUsed - if this is set to true, the corresponding item in the list should be rendered bold. (It should be noted here that this will only ever be viewed in FireFox.)
My experience is all in the middle \ backend tiers so the presentation layer is very new to me - can someone point me in the right direction? My initial thought was that somewhere in the custom control I would have access to all the rows that are returned from the data source which I could cycle through etc but I'm not sure if that's possible... There's also RenderContents which I can override... looks interesting!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是如何在代码隐藏中执行所需操作:
从
System.Web.UI.Control
继承的任何控件都具有属性Attributes
,您可以使用它来添加或附加style
属性。Here it is how to do what you need in code-behind:
Any control inherited from
System.Web.UI.Control
has propertyAttributes
which you can use to add or appendstyle
attribute.无论您在服务器端使用什么控件,它都会在客户端浏览器中呈现为 html,并且标准 html 下拉列表不支持对其内容进行样式设置。您可以使用 JavaScript 或 jQuery 自定义下拉列表控件来代替这样做。
Whatever the control that you are using in the server-side it will be rendered as a html in client browser, and the standard html drop-down list does not support styling its content. Instead of doing this you can go for JavaScript or jQuery custom drop-down list controls.
好吧,我想我已经回答了我自己的问题,但看起来不太优雅。
我可以编写一个新的存储过程来返回我需要在列表中显示的数据,该列表将返回 ID 和说明。但是,描述将是描述加上 TRUE 或 FALSE(取决于表中的 IsUsed 标志)。然后在 RenderContents 中,我可以拆分描述字符串,解析 bool 并添加样式属性,如果 bool 为 true,则使文本变为粗体...
OK I think I've answered my own question but it doesn't seem very elegant.
I can write a new stored proc to return the data I need to display in the list that will return ID and DESCRIPTION. However, description will be the description plus TRUE or FALSE (depending on the flag IsUsed in the table). Then in RenderContents I can split the description string, parse the bool and add a style attribute making the text bold if the bool is true...
我在 Chrome 中尝试了以下代码,它确实有效:(使顶部项目变为粗体。
I tried the following code in Chrome and it does work:(makes top item bold.