ASP Panel Control
The Panel Control is a container which houses other controls. Properties applied to the Panel control will also be applied to all the child controls. For example, if you hide the container, it hides all the controls in it.
- In this tutorial, you will learn how to use Panel Container and add controls to it.
- We will learn about the control alignment & how panel control re-arranges the controls when there is no room in a single row.
- The BackImageUrl property which will assign a background image to the Panel Control.
- Then, we will learn how we can show or hide a group of controls in a Panel Container.
1: ASP Panel and Control Alignment
In this section, we will examine how the ASP Panel Control aligns the controls in it. The steps are below:
1) The Panel control in the Toolbox is shown as a Square icon. First, create a Web Form called 09_PanelExample.aspx. Then, drag and drop the Panel container to the WebForm.
2) Once the Panel is placed, resize it so that it will have a size to hold two or three buttons in it. Refer to the box in the steps chart above to know the approximate size of the Panel.
3) Add Three New Buttons to the Panel. You can see by default; Panel is arranging the buttons from left to right.
4) When you place a new button on the Panel, it will be placed in the next row as there is no space in the current row. Note, we resized the panel in step 2 and the panel border shows its size. Since the panel’s HorizontalAlign property is Left by default, the newly placed control goes to the left edge in the second row.
5) Set the HorizontalAlign property to Right and observe the Control’s alignment. The Panel now arranged all the buttons from the right-left fashion. Also note the newly added button in the second row is towards the right edge of the panel.
6) Here, we Test how Panel arranges the buttons when its
HorizontalAlign property is set to Center.
2: Panel Background & Hide Controls
In this Sub-Step, we will set a background for the Panel. Later, we will see how to show/hide controls on the Web Form. Below are the steps:
1) Remove all the Panel Contents which we added in the previous step. Place a Button called Show. Set Text Property as Show and ID as btnShow.
2) Add the Hide ASP Button to the Form. Refer to the properties from the source tab entry shown above. Note, both Show and Hide buttons are outside the Panel control.
3) Set the width and height of the Panel Control as required by adjusting the grippers 3a and 3b.
4) Place three Labels and three text box controls inside the ASP Panel control. Note, all these 6 controls are child of the Panel.
5) Copy and paste the Panel to the paintbrush (Take Screenshot). Try to have the image size as same size of panel.
6) Next, delete the picture content in the paintbrush to erase all six controls. Then prepare the image as shown or the way you want. We will use this image as a background for the Panel. Save the image as PanelBackground.png
7) Add the image created in the previous step to the Image folder in the ASP Website. We did this many times in the previous labs.
8) The BackImageUrl property of the Panel allows us to see the background image for the Panel. In our case, we referred to the new image which we added to Image folder.
9) Switch to the design view of the Aspx form and you can see the Panel’s background is changed.
10) Now, we will handle the Click Event for both show and hide buttons. Double click on each of these buttons to get a handler function for both.
11) In the source view, you can verify the entry for onclick event.
12) Here, we set the Visible property of the panel control true on the show button handler. And, set the same property to false in the hide button’s click event handler. The Visible property decides whether to show the Panel or not. When we show or hide the Panel control, the Panel does the same for all its child controls. So, when the user clicks hide, the Panel hides all six controls from the web form. Note, the Show and Hide buttons are always visible as they are not part of the Panel control.
At this stage, you can run the web form and observe how the show or hide buttons are working by controlling the Panel’s visibility.
ASP Panel Control – Code Reference
<%@ Page Language="C#"
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
<form id="form1" runat="server">
<asp:Button ID="btnShow" runat="server"
Text="Show" onclick="btnShow_Click" />
<asp:Button ID="btnHide" runat="server" Text="Hide"
<asp:Panel ID="PanelC" runat="server" Height="164px" Width="262px"
<span class="style1">First Name :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br class="style1" />
<span class="style1">Middle Name :</span>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br class="style1" />
<span class="style1">Last Name : </span>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
public partial class _09_PanelExample : System.Web.UI.Page
//Sample 01: Show and Hide Panel
protected void btnShow_Click(object sender, EventArgs e)
PanelC.Visible = true;
protected void btnHide_Click(object sender, EventArgs e)
PanelC.Visible = false;
Categories: ASP 4.0
Tags: BackImageUrl, HorizontalAlign, onclick, Panel, Visible
Do you like this Example? Please comment about it for others!!