周围区块的最佳服务器控制
我想知道人们通常使用什么服务器控件来包围和分隔 Web 窗体场景中的相关控件和内容块。 很多时候,我需要用一个“面板”包围 HTML 块和相关的服务器控件,我可以根据后面代码中的某些 UI 逻辑来显示或隐藏该面板。 我通常面临使用面板的情况,但是如果我需要在表中包围多个 TR,则这不起作用,例如,因为面板呈现为 div,而在 Table 标记内无效。 在这种情况下,我通常只是将每个 TR 设置为在服务器上运行并单独显示/隐藏每个 TR,但必须有更好的方法。 我已经看到用于此场景的 PlaceHolder 控件,但想知道是否有人这样做或有任何其他建议。
I am wondering what server control people generally use for surrounding and separating blocks of related controls and content in a Web Forms scenario. Often times I need to surround a block of HTML and related Server controls with a 'panel' that I can show or hide based upon some UI logic in the code behind. I am usually faced with either using a Panel, however this doesn't work if I need to surround multiple TR's in a table for example because the Panel renders as a div which is invalid inside a Table tag. In this situation I usually just set each TR to runat server and show/hide each of them individually, but there must be a better way. I have seen the PlaceHolder control used for this scenario, but wanted to know if anyone does this or has any other suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为没有一个答案适用于所有场景 - 我们没有数百种工具以便我们始终可以使用锤子。
当生成的 div 可以正常工作时,面板非常有用。 PlaceHolder 继承自 Control 而不是 WebControl,因此您可以认为它使用的开销更少。 当我有一个表并且需要隐藏单个表行时,我也无需向 tr 添加 id 和 runat="server" 。
或者,当我需要制作一个巨大的编辑器控件表时(这适合 90% 的我想要隐藏单个表行的情况),我什至制作了一个可以自动执行一些标记的控件。 该控件自动执行字段标题文本,无论是否将其设为粗体(必需),添加带有翻转工具提示的帮助气泡,而我必须手动添加的只是文本框或其他编辑器控件。 积极的副作用是我的自定义控件为我提供了一个 ID,我也可以将其设置为 Visible。
I don't think there is a single answer that is true for all scenarios - we don't have hundreds of tools so that we can always use a hammer.
Panels are great for when a resulting div will work just fine. PlaceHolder inherits from Control instead of WebControl so you could make the argument that it uses less overhead. When I have a table and need to hide an individual table row, I have no compunctions with adding and id and runat="server" to the tr either.
Or, for times when I need to make a great big table of editor controls (this fits 90% of the times when I would want to hide a single table row) I've even made a control that automates some of the markup for me. That control automates the field header text, whether or not to make it bold (required), adds a help bubble with rollover tooltip, and all I have to add manually is the text box or other editor control. The positive side effect is that my custom control gives me an ID I can set Visible on as well.
即使使用 PlaceHolder 控件,Visual Studio 也会抱怨您没有有效的 HTML。
您可能会咬紧牙关,通过服务器端代码块来访问后面代码的属性:
但要小心...这是一个滑坡,您不希望您的 ASPX 太复杂。
Even with the PlaceHolder control, Visual Studio will complain that you don't have valid HTML.
You might grit your teeth and do it via server side code blocks that access properties on the code behind:
Be careful though... it's a slippery slope and you don't want your ASPX to be too convoluted.
也许是字面控制。
Perhaps a literal control.
您可以使用任何您想要的 html 元素,只需给它一个 id 和 runat 服务器即可。 这给了你一些灵活性。
You can use any html element you want, just give it an id and runat server. That gives you some flexibility.