Partager via


Panel Web Server Control

Provides a container for other controls. This control is rendered as an HTML <div> element.

<asp:Panel id="Panel1" 
     BackImageUrl="url"
     HorizontalAlign="Center|Justify|Left|NotSet|Right" 
     Wrap="True|False"
     runat="server">

   (Other controls declared here)

</asp:Panel>

Remarks

The Panel control is a container for other controls. It is especially useful for generating controls programmatically and displaying and hiding groups of controls. You can display an image in the background of the Panel control by setting the BackImageUrl property. Using the HorizontalAlignment property, you can specify the horizontal alignment of the items contained in the control. The Wrap property lets you determine whether items in the control automatically continue on the next line when a line is longer than the width of the panel.

For detailed information on the Panel Web server control's properties and events, see the Panel Class documentation.

Example

The following example demonstrates how to use a Panel control to display and hide a group of controls.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub Page_Load(sender As Object, e As EventArgs)
         ' Show/Hide Panel Contents
         If Check1.Checked Then
            Panel1.Visible = False
         Else
            Panel1.Visible = True
         End If
        
         ' Generate label controls
         Dim numlabels As Integer = Int32.Parse(DropDown1.SelectedItem.Value)
         Dim i As Integer
         For i = 1 To numlabels
            Dim l As New Label()
            l.Text = "Label" + i.ToString()
            l.ID = "Label" + i.ToString()
            Panel1.Controls.Add(l)
            Panel1.Controls.Add(New LiteralControl("<br>"))
         Next i
        
         ' Generate textbox controls.
         Dim numtexts As Integer = Int32.Parse(DropDown2.SelectedItem.Value)
         For i = 1 To numtexts
            Dim t As New TextBox()
            t.Text = "TextBox" & i.ToString()
            t.ID = "TextBox" & i.ToString()
            Panel1.Controls.Add(t)
            Panel1.Controls.Add(New LiteralControl("<br>"))
         Next i
      End Sub
   </script>
</head>
<body>
   <h3>Panel Example</h3>
   <form runat="server">
      <asp:Panel id="Panel1" runat="server"
           BackColor="gainsboro"
           Height="200px"
           Width="300px">
 
           Panel1: Here is some static content...
           <p>
      </asp:Panel>
      <p>
      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>
      </asp:DropDownList>
      <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>
      </asp:DropDownList>
      <p>
      <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>
      <p>
      <asp:Button Text="Refresh Panel" runat="server"/>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void Page_Load(Object sender, EventArgs e) 
      {
         // Show/Hide Panel Contents.
         if (Check1.Checked) 
         {
            Panel1.Visible=false;
         }
         else 
         {
            Panel1.Visible=true;
         }
 
         // Generate 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(l);
            Panel1.Controls.Add(new LiteralControl("<br>"));
         }
 
         // Generate 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(t);
            Panel1.Controls.Add(new LiteralControl("<br>"));
         }
      }
   </script>
</head>
<body>
   <h3>Panel Example</h3>
   <form runat="server">
      <asp:Panel id="Panel1" runat="server"
           BackColor="gainsboro"
           Height="200px"
           Width="300px">
           Panel1: Here is some static content...
           <p>
      </asp:Panel>
      <p>
      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>
      </asp:DropDownList>
      <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>
      </asp:DropDownList>
      <p>
      <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>
      <p>
      <asp:Button Text="Refresh Panel" runat="server"/>
   </form>
</body>
</html>

See Also

Web Server Controls | Panel Class