New Document
Panel Control in ASP.Net

The Panel control works as a container for other controls on the page. It controls the appearance and visibility of the controls it contains. It also allows generating controls programmatically.

The basic syntax for the Panel control is as follows:

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

The Panel control is used as a container for other controls. This control is often used to generate controls by code and to display or hide groups of controls as per the condition.

Note: This control always renders as an HTML < div> element.

<asp:Panel ID="pnlMain" runat="server"
        BackImageUrl="~/Images/FunnyScraps3.jpg"
        GroupingText="This is Sample Group Text"
        HorizontalAlign="Center"
        ScrollBars="Both"
        Direction="LeftToRight"
        Wrap="true"
        DefaultButton="btnSave"
        Height="400px" Width="500px">
       <table>
            <tr>
                <td colspan="2">
                    The Panel control is used as a container for other controls. This control is often used to generate controls by code and to display and hide groups of controls.
               </td>
            </tr>
            <tr>
                <td>User Name</td>
                <td><asp:TextBox ID="txtUserID" runat="server" /></td>
            </tr>
            <tr>
                <td>User Password</td>
               <td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password" /></td>
            </tr>
            <tr>
                <td>Confirm Password</td>
                <td><asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"/></td>
            </tr>
            <tr>
                <td></td>
               <td>
                    <asp:Button ID="btnSave" runat="server" Text="Save" />
                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
               </td>
           </tr>
       </table>
</asp:Panel>

The Panel control is derived from the WebControl class. So it inherits all the properties, methods and events of the same. It does not have any method or event of its own. However it has the following properties of its own:

PropertiesDescription
BackImageUrlURL of the background image of the panel.
DefaultButtonGets or sets the identifier for the default button that is contained in the Panel control.
DirectionText direction in the panel.
GroupingTextAllows grouping of text as a field.
HorizontalAlignHorizontal alignment of the content in the panel.
ScrollBarsSpecifies visibility and location of scrollbars within the panel.
WrapAllows text wrapping.
Working with the Panel Control:

Let us start with a simple scrollable panel of specific height and width and a border style. The ScrollBars property is set to both, so both the scrollbars are rendered.

The source file has the following code for the panel tag:

<asp:Panel ID="Panel1" runat="server" 
           BorderColor="#990000" 
           BorderStyle="Solid" 
           BorderWidth="1px" 
           Height="116px" 
           ScrollBars="Both" 
           Width="278px">
This is a scrollable panel.
<br />
<br />
<asp:Button ID="btnpanel" 
            runat="server" 
            Text="Button" 
            Width="82px" />
</asp:Panel>

The panel is rendered as followes:

Panel
Example:

The following example demonstrates dynamic content generation. The user provides the number of label controls and textboxes to be generated on the panel. The controls are generated programmatically.

Change the properties of the panel using the properties window. When you select a control on the design view the properties window displays the properties of that particular control and allows you to make changes, without typing.

Panel2

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" 
           BorderWidth="1px" 
           Height="150px" 
           ScrollBars="Auto" 
           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" 
            Width="129px" />
</td>
</tr>
</table>
</form>

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

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


Previous                                                                                                                                                       Next

Back to Top