Panel控件和Div的区别
我想知道asp.net中的Panel
控件和带有runat="server"
的div
之间有什么区别?因为两者都呈现为 div
。
哪一个最好(条件)?
I want to know what is difference between Panel
control in asp.net and div
with runat="server"
? Since both render as a div
.
Which one is best (conditions)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码
与您所做的完全相同:
它们呈现相同的效果,但面板最常用的是其他 WebControls 的功能,并且面板 Web 控件为您提供了在代码隐藏下的更多控制因为它暴露了更多的属性。
The code
is exactly the same as if you do:
They render the same, but it's the functionality with other WebControls that the Panel is most used, and the Panel web control gives you more control under code-behind as it exposes more properties.
不同之处在于
Panel
是一个 Web 控件,它将在代码隐藏文件中为您提供比 div 更多的属性,因为它是一个 Web 控件,因此需要更多处理来生成 HTML。面板控件具有 viewstate 属性,而
div
则没有。这实际上取决于您的使用情况。如果您希望控制更多属性,请使用面板控件,否则使用 div 控件。
The difference is that
Panel
is a webcontrol which will give you more properties over div in the code behind file, since it's a webcontrol it will require more processing to generate HTML.The panel control has the viewstate property while
div
does not.It really depends on your usage. If you prefer to have control over more properties, then use the panel control, otherwise use the div control.
我发现的最有用的区别是:即使使用 runat=server,div 也不会在页面服务之间保留对其样式的更改。这让我抓狂,我有一个 div 持有一个用于弹出 aspx 屏幕的 iframe。我希望通过 javascript 将可见性设置为无,当用户完成此弹出窗口时关闭该弹出窗口。我发现它不断重新出现,即使我试图在每个页面服务的隐藏页面上断言代码可见性时也是如此。
然后我切换到使用 asp:panel ,并且由于它的视图状态,您设置了它的可见性,并且它在多个页面服务中保持这种状态,直到您再次更改它。干净多了。您仍然可以将 css 样式应用于该面板,与 div 相同,但它的“行为”更好
The most useful distinction I have found is this; A div, even with runat=server will not persist changes to its style between page serves. This was driving me nuts, I had a div holding an iframe for a pop-up aspx screen. I wanted this pop-up to close when user was finished with it by setting the visibility to none via javascript. I found it kept re-appearing even when I tried to assert visibility in code behind on each page serve for the holding page.
I then switched to using asp:panel and because of its viewstate, you set it's visibility and it STAYS THAT WAY through multiple page serves until you change it again. Much cleaner. You can still apply css style to that panel, same as a div, but its better 'behaved'