Export (0) Print
Expand All
0 out of 37 rated this helpful - Rate this topic

Panel Class

Represents a control that acts as a container for other controls.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class Panel : WebControl
public class Panel extends WebControl
public class Panel extends WebControl
Not applicable.

The Panel control is a container for other controls. It is especially useful when you want to generate controls programmatically, hide/show a group of controls, or localize a group of controls.

The Direction property is useful for localizing a Panel control's content to display text for languages that are written from right to left, such as Arabic or Hebrew.

The Panel control provides several properties that allow you to customize the behavior and display of its contents. Use the BackImageUrl property to display a custom image for the Panel control. Use the ScrollBars property to specify scroll bars for the control.

The following example illustrates the use of a Panel control to generate controls programmatically and to hide/show a group of controls.


The following code sample uses the single-file code model and may not work correctly if copied directly into a code-behind file. This code sample must be copied into an empty text file that has an .aspx extension. For more information on the Web Forms code model, see ASP.NET Web Page Code Model.

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
    <title>Panel Example</title>
<script runat="server">
       void Page_Load(Object sender, EventArgs e) {
          // Show or hide the Panel contents.
          if (Check1.Checked) {
          else {
          // Generate the Label controls.
          int numlabels = Int32.Parse(DropDown1.SelectedItem.Value);
          for (int i=1; i<=numlabels; i++) {
             Label l = new Label();
             l.Text = "Label" + (i).ToString();
             l.ID = "Label" + (i).ToString();
             Panel1.Controls.Add(new LiteralControl("<br />"));
          // Generate the Textbox controls.
          int numtexts = Int32.Parse(DropDown2.SelectedItem.Value);
          for (int i=1; i<=numtexts; i++) {
             TextBox t = new TextBox();
             t.Text = "TextBox" + (i).ToString();
             t.ID = "TextBox" + (i).ToString();
             Panel1.Controls.Add(new LiteralControl("<br />"));
    <h3>Panel Example</h3>
    <form id="form1" runat="server">
       <asp:Panel id="Panel1" runat="server"
            Panel1: Here is some static content...
            <br />
       <br />
       Generate Labels:
       <asp:DropDownList id="DropDown1" runat="server">
          <asp:ListItem Value="0">0</asp:ListItem>
          <asp:ListItem Value="1">1</asp:ListItem>
          <asp:ListItem Value="2">2</asp:ListItem>
          <asp:ListItem Value="3">3</asp:ListItem>
          <asp:ListItem Value="4">4</asp:ListItem>
       <br />
       Generate TextBoxes:
       <asp:DropDownList id="DropDown2" runat="server">
          <asp:ListItem Value="0">0</asp:ListItem>
          <asp:ListItem Value="1">1</asp:ListItem>
          <asp:ListItem Value="2">2</asp:ListItem>
          <asp:ListItem Value="3">3</asp:ListItem>
          <asp:ListItem Value="4">4</asp:ListItem>
       <br />
       <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>
       <br />
       <asp:Button Text="Refresh Panel" runat="server"/>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.