ASP.NET - 面板控制元件



面板控制元件充當頁面上其他控制元件的容器。它控制其包含控制元件的外觀和可見性。它還允許以程式設計方式生成控制元件。

面板控制元件的基本語法如下:

<asp:Panel ID= "Panel1"  runat = "server">
</asp:Panel>

面板控制元件派生自 WebControl 類。因此,它繼承了該類的所有屬性、方法和事件。它本身沒有任何方法或事件。但是,它具有以下自己的屬性:

屬性 描述
BackImageUrl 面板背景影像的 URL。
DefaultButton 獲取或設定面板控制元件中包含的預設按鈕的識別符號。
Direction 面板中的文字方向。
GroupingText 允許將文字分組為一個欄位。
HorizontalAlign 面板中內容的水平對齊方式。
ScrollBars 指定面板內捲軸的可見性和位置。
Wrap 允許文字換行。

使用面板控制元件

讓我們從一個具有特定高度和寬度以及邊框樣式的簡單可滾動面板開始。ScrollBars 屬性設定為兩個捲軸,因此呈現兩個捲軸。

原始檔包含面板標籤的以下程式碼:

<asp:Panel ID="Panel1" runat="server" BorderColor="#990000" BorderStyle="Solid" 
   Borderstyle="width:1px" Height="116px" ScrollBars="Both" style="width:278px">
   
   This is a scrollable panel.
   <br />
   <br />

   <asp:Button ID="btnpanel" runat="server" Text="Button" style="width:82px" />
</asp:Panel>

面板呈現如下:

Panel

示例

以下示例演示動態內容生成。使用者提供要在面板上生成的標籤控制元件和文字框的數量。控制元件以程式設計方式生成。

使用屬性視窗更改面板的屬性。當您在設計檢視中選擇一個控制元件時,屬性視窗會顯示該特定控制元件的屬性,並允許您進行更改而無需鍵入。

Panel2

示例的原始檔如下:

<form id="form1" runat="server">
   <div>
      <asp:Panel ID="pnldynamic" runat="server" BorderColor="#990000" 
         BorderStyle="Solid" Borderstyle="width:1px" Height="150px"  ScrollBars="Auto" style="width:60%" BackColor="#CCCCFF"  Font-Names="Courier" HorizontalAlign="Center">
     
         This panel shows dynamic control generation:
         <br />
         <br />
      </asp:Panel>
   </div>

   <table style="width: 51%;">
      <tr>
         <td class="style2">No of Labels:</td>
         <td class="style1">
            <asp:DropDownList ID="ddllabels" runat="server">
               <asp:ListItem>0</asp:ListItem>
               <asp:ListItem>1</asp:ListItem>
               <asp:ListItem>2</asp:ListItem>
               <asp:ListItem>3</asp:ListItem>
               <asp:ListItem>4</asp:ListItem>
            </asp:DropDownList>
         </td>
      </tr>

      <tr>
         <td class="style2"> </td>
         <td class="style1"> </td>
      </tr>

      <tr>
         <td class="style2">No of Text Boxes :</td>
         <td class="style1">
            <asp:DropDownList ID="ddltextbox" runat="server">
               <asp:ListItem>0</asp:ListItem>
               <asp:ListItem Value="1"></asp:ListItem>
               <asp:ListItem>2</asp:ListItem>
               <asp:ListItem>3</asp:ListItem>
               <asp:ListItem Value="4"></asp:ListItem>
            </asp:DropDownList>
         </td>
      </tr>

      <tr>
         <td class="style2"> </td>
         <td class="style1"> </td>
      </tr>

      <tr>
         <td class="style2">
            <asp:CheckBox ID="chkvisible" runat="server" 
               Text="Make the Panel Visible" />
         </td>

         <td class="style1">
            <asp:Button ID="btnrefresh" runat="server" Text="Refresh Panel" 
               style="width:129px" />
         </td>
      </tr>
   </table>
</form>

Page_Load 事件背後的程式碼負責動態生成控制元件。

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      //make the panel visible
      pnldynamic.Visible = chkvisible.Checked;

      //generating the lable controls:
      int n = Int32.Parse(ddllabels.SelectedItem.Value);
      for (int i = 1; i <= n; i++)
      {
         Label lbl = new Label();
         lbl.Text = "Label" + (i).ToString();
         pnldynamic.Controls.Add(lbl);
         pnldynamic.Controls.Add(new LiteralControl("<br />"));
      }
      
      //generating the text box controls:

      int m = Int32.Parse(ddltextbox.SelectedItem.Value);
      for (int i = 1; i <= m; i++)
      {
         TextBox txt = new TextBox();
         txt.Text = "Text Box" + (i).ToString();
         pnldynamic.Controls.Add(txt);
         pnldynamic.Controls.Add(new LiteralControl("<br />"));
      }
   }
}

執行後,面板呈現為:

Panel3
廣告
© . All rights reserved.