Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET Panel control


May 13, 2021 ASP.NET


Table of contents


Panel control

Panel controls can be containers for other controls on a page. I t controls the appearance and visibility of the controls it contains. It also allows the build control to be programmed.

The basic syntax of panel controls is as follows:

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

Panel controls are derived from the WebControl class. T herefore, it inherits all the properties, methods, and events as well. I t does not have any methods or events of its own. However, it has its own following properties:

Property Describe
BackImageUrl The address of the panel background image.
DefaultButton Gets or sets the identifier of the default button contained in the Panel control.
Direction The orientation of the text in the panel.
GroupingText Allows text to be grouped as a field.
HorizontalAlign Align the contents of the panel horizontally.
ScrollBars Specifies the visibility and position of the scroll bar within the panel.
Wrap Allow text to line.

Use panel controls

Let's start with a simple scroll panel with a specific height and width and border style. The scroll bar property is set to two scroll bars, so both scroll bars are rendered at the same time.

The source file has the following panel label code:

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

The panel is rendered as follows:

ASP.NET Panel control

Example

The following example demonstrates dynamic content generation. T he number of label controls and text boxes that the user provides to be generated on the panel. Control is programmatically generated.

Use the property window to change panel properties. When you select a control in design view, the properties of a particular control are displayed in the property window and allow you to change it without typing.

ASP.NET Panel control

The source file for the example is as follows:

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

The source code Page_Load responsible for dynamically generating the control behind the event is:

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 />"));
      }
   }
}

When executed, the panel is rendered as:

ASP.NET Panel control