当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Web服务器控件:Panel控件

ASP.NET
asp.net Linq TO Sql 分页方法
asp.net 用XML生成放便扩展的自定义树
asp.ent下合并两个结构相同的DataTable
asp.net 遍历repeater中的控件的几种方式
asp.net 处理原文件中过长的viewstate代码
asp.net下遍历页面中所有的指定控件的代码
获取创建Membership的数据库创建脚本
asp.net AJAX注册类
asp.net 处理F5刷新页面重复提交页面的一个思路
ASP.NET 缓存分析和实践浅析提高运行效率
asp.net 读取并显示excel数据的实现代码
ASP.NET中常用的用来输出JS脚本的类
ASP.NET中内嵌页面代码的一个问题
asp.net(C#)操作excel(上路篇)
一个基于Asp.Net MVC的权限方案
ASP.NET实例教程:51job网站地区选择功能
ASP.NET教程:友好的Html和JS适合SEO
ASP.NET教程:使用.ashx文件去除重复内容
ASP.NET做SEO:制作架构清晰和更新及时的网站地图
ASP.NET优化:Sql注入和Html注入的黑帽SEO

ASP.NET 中的 Web服务器控件:Panel控件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 38 ::
收藏到网摘: n/a

阅读此文请先查看软晨学习网的:ASP.NET入门教程:Web服务器控件,简单讲述了Web服务器控件的使用方法。

定义和用法

Panel 控件用作其它控件的容器。

提示:此控件常用于以编程方式生成控件,或显示或隐藏控件组。

注释:在 IE 中,此控件呈现为 HTML 的 <div> 元素,在 Mozilla 中呈现为 <table> 标签。

属性

属性 描述 .NET
BackImageUrl 规定显示控件背景的图像文件的 URL。 1.0
DefaultButton 规定 Panel 中默认按钮的 ID。 2.0
Direction 规定 Panel 的内容显示方向。 2.0
GroupingText 规定 Panel 中控件组的标题。 2.0
HorizontalAlign 规定内容的水平对齐方式。 1.0
runat 规定控件是服务器。必须设置为 "server"。 1.0
ScrollBars 规定 Panel 中滚动栏的位置和可见性。 2.0
Wrap 规定内容是否折行。 1.0

Web 控件标准属性

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"
    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 控件显示和隐藏一组控件。

下面的代码示例使用单文件代码模型,如果直接将该代码示例复制到代码隐藏文件,则可能无法正常运行。必须将此代码示例复制到扩展名为 .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>