Panel 控件用作其它控件的容器。
提示:此控件常用於以編程方式生成控件,或顯示或隱藏控件組。
注釋:在 IE 中,此控件呈現為 Html 的 <div> 元素,在 Mozilla 中呈現為 <table> 標簽。
AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CSSClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableVIEwState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
<ASP:Panel
AccessKey="string"
BackColor="color name|#dddddd"
BackImageUrl="uri"
BorderColor="color name|#dddddd"
BorderPADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px"> 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"
TabIndex="integer"
ToolTip="string"
Visible="True|False"
Width="size"
Wrap="True|False"
/>
備注:Panel 控件是其他控件的容器。它對於以編程方式生成控件以及顯示和隱藏控件組尤其有用。通過設置 BackImageUrl 屬性,可在 Panel 控件的背景中顯示一個圖像。使用 HorizontalAlignment 屬性可以指定包含在該控件中的項的水平對齊方式。Wrap 屬性使您可以確定當行的長度超過面板的寬度時,該控件中的項是否自動在下一行繼續。
下面的示例演示如何使用 Panel 控件顯示和隱藏一組控件。
下面的代碼示例使用單文件代碼模型,如果直接將該代碼示例復制到代碼隱藏文件,則可能無法正常運行。必須將此代碼示例復制到擴展名為 .ASPx 的空文本文件中。
Visual Basic
<%@ Page Language="VB" AutoEventWireup="True" %>
<Html>
<head>
<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 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 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 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>