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 of panel control is as follows:
<asp:Panel ID= "Panel1" runat = "server"> </asp:Panel>
The Panel control is derived from the WebControl class. Hence 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:
Properties | Description |
---|---|
BackImageUrl | URL of the background image of the panel. |
DefaultButton | Gets or sets the identifier for the default button that is contained in the Panel control. |
Direction | Text direction in the panel. |
GroupingText | Allows grouping of text as a field. |
HorizontalAlign | Horizontal alignment of the content in the panel. |
ScrollBars | Specifies visibility and location of scrollbars within the panel. |
Wrap | Allows text wrapping. |
Let us start with a simple scrollable panel of specific height and width and a border style. The ScrollBars property is set to both the scrollbars, hence 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" 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:
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.
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 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 />")); } } }
When executed, the panel is rendered as: