Share via


Panel Web 伺服器控制項宣告式語法

更新:2007 年 11 月

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

 <asp:Panel     AccessKey="string"     BackColor="color name|#dddddd"     BackImageUrl="uri"     BorderColor="color name|#dddddd"     BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|         Inset|Outset"     BorderWidth="size"     CssClass="string"     DefaultButton="string"     Direction="NotSet|LeftToRight|RightToLeft"     Enabled="True|False"     EnableTheming="True|False"     EnableViewState="True|False"     Font-Bold="True|False"     Font-Italic="True|False"     Font-Names="string"     Font-Overline="True|False"     Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|         Large|X-Large|XX-Large"     Font-Strikeout="True|False"     Font-Underline="True|False"     ForeColor="color name|#dddddd"     GroupingText="string"     Height="size"     HorizontalAlign="NotSet|Left|Center|Right|Justify"     ID="string"     OnDataBinding="DataBinding event handler"     OnDisposed="Disposed event handler"     OnInit="Init event handler"     OnLoad="Load event handler"     OnPreRender="PreRender event handler"     OnUnload="Unload event handler"     runat="server"     ScrollBars="None|Horizontal|Vertical|Both|Auto"     SkinID="string"     Style="string"     TabIndex="integer"     ToolTip="string"     Visible="True|False"     Width="size"     Wrap="True|False" />

備註

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

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

範例

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

注意事項:

下列程式碼範例使用單一檔案程式碼模型,若直接複製到程式碼後置檔案,可能無法正確運作。這個程式碼範例必須複製到 .aspx 副檔名的空文字檔中。如需 Web Form 的詳細資訊,請參閱 ASP.NET Web 網頁程式碼模型

<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>Panel Example</title>
<script runat="server">

    Sub Page_Load(sender As Object, e As EventArgs)

        ' Show or Hide the Panel contents.
        If Check1.Checked Then
            Panel1.Visible = False
        Else
            Panel1.Visible = True
        End If

        ' Generate the 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 the 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 id="form1" runat="server">

       <asp:Panel id="Panel1" runat="server"
            BackColor="gainsboro"
            Height="200px"
            Width="300px">

            Panel1: Here is some static content...
            <br />

       </asp:Panel>

       <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>
       </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>

       <br />
       <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>

       <br />
       <asp:Button Text="Refresh Panel" runat="server"/>


    </form>

 </body>
 </html>

<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head>
    <title>Panel Example</title>
<script runat="server">

       void Page_Load(Object sender, EventArgs e) {

          // Show or hide the Panel contents.

          if (Check1.Checked) {
             Panel1.Visible=false;
          }
          else {
             Panel1.Visible=true;
          }

          // 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(l);
             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(t);
             Panel1.Controls.Add(new LiteralControl("<br />"));
          }
       }

    </script>

 </head>
 <body>

    <h3>Panel Example</h3>

    <form id="form1" runat="server">

       <asp:Panel id="Panel1" runat="server"
            BackColor="gainsboro"
            Height="200px"
            Width="300px">

            Panel1: Here is some static content...
            <br />

       </asp:Panel>

       <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>
       </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>

       <br />
       <asp:CheckBox id="Check1" Text="Hide Panel" runat="server"/>

       <br />
       <asp:Button Text="Refresh Panel" runat="server"/>


    </form>

 </body>
 </html>

請參閱

參考

Panel

其他資源

Web 伺服器控制項語法