44

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)?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Viren
  • 649
  • 2
  • 6
  • 9

3 Answers3

37

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.

balexandre
  • 73,608
  • 45
  • 233
  • 342
  • What's a common example of the extra `Panel` functionality that the `div` would lose? And here I'm most curious in things that aren't simple wrappings -- that is, I bet [`BackColor`](https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.webcontrol.backcolor?view=netframework-4.8#System_Web_UI_WebControls_WebControl_BackColor) just reduces to some CSS. How is a `Panel` useful when coding? Are there code behind linkages that are commonly used, eg? – ruffin Nov 25 '19 at 23:35
9

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.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Atul Phadtare
  • 555
  • 3
  • 7
  • 13
0

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'

Julian
  • 1
  • 1