ASP.NET Labels render as HTML
<span> </span>
elements.
ASP.NET Panels render as HTML
<div> </div>
elements.
An HTML
<span>
element has a
CSS display type of "inline" by default; whereas an HTML
<div>
element has a CSS display type of "block" by default.
Because the
<span>
is inline, you cannot set the height and width of it. It automatically adjusts it's size according to the contents that are within it: making it "inline".
But the
<div>
is displayed as a block, so you can set the height and width of it.
If you set the height and width of an element that has a display type of block, the content inside it will not change the height and width of that element.
That is where the
overflow style comes into play.
If you set the CSS overflow to "scroll" for a block element, scroll bars will be provided so that the user can scroll the content that is too large to fit in the defined area. If you specify that the overflow should be "hidden" then the content will be cut off where it doesn't fit. If it's set to "visible" then the content will appear outside of the defined area.
So, now back to the question: How do you display scroll bars for a grid that is too large for the page?
The answer is to wrap the GridView inside an element that has a CSS
display:block
style.
Since ASP.NET Panels are rendered HTML
<div>
elements... and since
<div>
elements have a block style by default. All you have to do is set the the Panel's height and width to the desired dimensions and set it's CSS style to have
overflow:scroll
.
You don't have to use an ASP.NET Panel if you don't plan on working with the Panel in your server side code. You could simply wrap the GridView in a
<div>
element and set it's dimensions and overflow styles.
Here's an example with a Panel:
-
<asp:Panel ID="scrollableGridArea" runat="server" style="overflow:scroll; height: 50px; width: 50px">
-
<asp:GridView ID="myGrid" runat="server />
-
</asp:Panel>
Here's an example using a
<div>
:
-
<div style="overflow:scroll; height: 50px; width: 50px">
-
<asp:GridView ID="myGrid" runat="server />
-
</div>
If you really want to use a Label or
<span>
for some crazy reason, you can set the CSS display:block. But it's really pointless when there's the
div
/Panel.
-Frinny