Thursday, 30 January 2014

ASP.NET - Panel Controls

ASP.NET - Panel Controls


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 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" 
           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 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" 
           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:
Panel3

0 comments:

Post a Comment