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)?
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)?
The code
<asp:Panel id="abc" runat="server">
is exactly the same as if you do:
<div id="abc" runat="server">
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.
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.
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'