๐๏ธ ASP.NET โ Panel Controls โ Beginnerโs Guide to Grouping Web Controls with Code & Output
๐งฒ Introduction โ What Is an ASP.NET Panel Control?
The ASP.NET Panel control is a container control used to group other controls together in a Web Forms page. It helps developers organize layout, apply styles, and show or hide multiple controls at once.
You can think of a Panel like a div tag in HTML, but with extra server-side capabilities:
- You can add other controls inside a Panel
- You can control its visibility or appearance using C# code
- You can apply borders, padding, or background color
๐ฏ In this guide, you’ll learn:
- What Panel controls are and how they work
- How to use Panels to organize a form
- How to show/hide content inside a Panel using C#
- Full code example with beginner-friendly explanations and output
๐ ASP.NET Web Forms File Structure
| ๐ File Name | ๐ Description |
|---|---|
PanelDemo.aspx | Frontend layout with Panel and controls |
PanelDemo.aspx.cs | Code-behind file to control Panel behavior using C# |
๐ Step 1: Markup File โ PanelDemo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PanelDemo.aspx.cs" Inherits="PanelDemo.PanelDemo" %>
<html>
<body>
<form id="form1" runat="server">
<h3>๐๏ธ ASP.NET Panel Control Demo</h3>
<!-- Toggle Button -->
<asp:Button ID="btnToggle" runat="server" Text="Show/Hide Panel" OnClick="btnToggle_Click" /><br /><br />
<!-- Panel Control -->
<asp:Panel ID="pnlUserInfo" runat="server" BorderStyle="Solid" BorderWidth="1px" Padding="10" BackColor="#f0f0f0">
<asp:Label ID="lblName" runat="server" Text="Enter your name:" /><br />
<asp:TextBox ID="txtName" runat="server" /><br /><br />
<asp:Label ID="lblAge" runat="server" Text="Enter your age:" /><br />
<asp:TextBox ID="txtAge" runat="server" /><br /><br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</asp:Panel>
<br /><br />
<asp:Label ID="lblResult" runat="server" ForeColor="Blue" />
</form>
</body>
</html>
๐ Explanation of Panel Markup
| ๐งฉ Control / Tag | ๐ฌ Description |
|---|---|
<asp:Panel ... /> | Groups related controls into one container |
BorderStyle="Solid" | Adds a solid border around the panel |
BackColor="#f0f0f0" | Gives a light gray background to distinguish it |
Padding="10" | Adds spacing inside the panel |
btnToggle | Button to show or hide the entire panel on click |
btnSubmit | Submits the form and shows result outside the panel |
โ๏ธ Step 2: Code-Behind File โ PanelDemo.aspx.cs
public partial class PanelDemo : System.Web.UI.Page
{
protected void btnToggle_Click(object sender, EventArgs e)
{
// Toggle panel visibility
pnlUserInfo.Visible = !pnlUserInfo.Visible;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Get user inputs
string name = txtName.Text;
string age = txtAge.Text;
// Display result
lblResult.Text = $"Hello, {name}! You are {age} years old.";
}
}
๐ Code Explanation for Beginners
| ๐ง Line / Concept | ๐ก What It Does |
|---|---|
pnlUserInfo.Visible | Controls whether the panel is visible on the page |
!pnlUserInfo.Visible | ! means “not” โ it flips the current state (if visible, hides it; if hidden, shows it) |
txtName.Text and txtAge.Text | Fetch values typed in the textboxes |
lblResult.Text | Displays a message below the panel |
๐ฅ๏ธ Output Preview in Browser
๐น On Page Load:
[Show/Hide Panel Button]
-------------------------
| Enter your name: |
| [__________] |
| Enter your age: |
| [__________] |
| [Submit] |
-------------------------
[Result Label]
๐น After Entering:
Name: Amit
Age: 24
Then clicking Submit:
Hello, Amit! You are 24 years old.
๐น Clicking Show/Hide Panel Button:
The entire form inside the panel appears or disappears.
๐ Summary โ Recap & Takeaways
ASP.NET Panel control is a powerful layout tool that:
๐ Key Learnings:
- Groups multiple controls into one container
- Makes it easier to apply a shared background, border, or padding
- Allows you to show/hide all enclosed controls at once
- Can be used with event logic in C# to toggle visibility
- Supports all standard Web Forms controls inside it
โ Use panels for:
- Organizing sections of a form
- Tab-based layouts
- Pop-up divs
- Admin dashboard sections
- Reusable visual containers
โ Frequently Asked Questions (FAQs)
โ What is the difference between a Panel and a DIV?
โ
An HTML <div> is purely visual unless styled with CSS or controlled by JavaScript. An <asp:Panel> gives you server-side access (in C#), so you can show, hide, or update it during events.
โ Can I add controls inside a Panel dynamically?
โ
Yes. You can use C# in the code-behind to create controls and add them like this:
TextBox tb = new TextBox();
pnlUserInfo.Controls.Add(tb);
โ How do I hide the Panel when a button is clicked?
โ
Just set:
pnlUserInfo.Visible = false;
โ Does the Panel keep its state between postbacks?
โ
Yes. Its visibility and contents are preserved thanks to ViewState unless disabled explicitly.
Share Now :
