共用方式為


Panel Web 伺服器控制項

為其他控制項提供容器。這個控制項是呈現為 HTML <div> 項目。

<asp:Panelid="Panel1"      BackImageUrl="url"     HorizontalAlign="Center|Justify|Left|NotSet|Right"      Wrap="True|False"     runat="server">   (Other controls declared here)</asp:Panel>

備註

Panel 控制項是其他控制項的容器。它對於以程式設計方式產生控制項以及顯示和隱藏控制項群組尤其有用。您可以設定 BackImageUrl 屬性,在 Panel 控制項的背景中顯示影像。您可以使用 HorizontalAlignment 屬性來指定控制項中所含項目的水平對齊方式。Wrap 屬性可以讓您決定控制項中的項目,要不要在一行的長度超過面板寬度時自動從下一行繼續。

如需 Panel Web 伺服器控制項之屬性和事件的詳細資訊,請參閱 Panel 類別文件。

範例

下列範例是示範如何使用 Panel 控制項來顯示和隱藏控制項群組。

<%@ 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>

請參閱

Web 伺服器控制項 | Panel 類別