Introduction

This article demonstrates how to create and use a Panel Control in ASP.NET with C#. This article starts with the introduction of the panel in ASP.NET. After that, it demonstrates how to position the click event handler of a panel.

Creating a Panel

The Panel tag is,

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

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.

Steps

The source code snippet creates a panel control and sets the name and content of a Pannel control.

We can take an example of online payment mode for panel control in this. There are 3-4 panels; after selecting any option the respective panel appears on screen.

So if you select the 1st radio button then it will show you panel 1 ,similarly if you select the 2nd radio button then it will show you panel 2 , if you select the 3rd radio button then it will show you panel 3, and if you select the 4th radio button then it will show you panel 4.

Codes

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Pannel Control</title>
  6. <style type="text/css">
  7. .style1
  8. {
  9. text-align: center;
  10. background-color: #999999;
  11. }
  12. .style2
  13. {
  14. background-color: #999999;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <form id="form1" runat="server">
  20. <div>
  21. <table class="style3" align="center">
  22. <tr>
  23. <td class="style1">
  24. <strong style="background-color: #999999">Payment Mode</strong></td>
  25. </tr>
  26. <tr>
  27. <td class="style2">
  28. <asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"
  29. onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
  30. <asp:ListItem>Via Debit Card</asp:ListItem>
  31. <asp:ListItem>Via Credit Card</asp:ListItem>
  32. <asp:ListItem>Via Internet Banking</asp:ListItem>
  33. <asp:ListItem>Via Cash On Delivery</asp:ListItem>
  34. </asp:RadioButtonList>
  35. </td>
  36. </tr>
  37. <tr>
  38. <td class="style2">
  39. <asp:Panel ID="Panel1" runat="server" CssClass="style2">
  40. Bank
  41. <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
  42. <asp:ListItem>SBI</asp:ListItem>
  43. <asp:ListItem>PNB</asp:ListItem>
  44. <asp:ListItem>CBI</asp:ListItem>
  45. <asp:ListItem>BOI</asp:ListItem>
  46. </asp:DropDownList>
  47. <br />
  48. Card No
  49. <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
  50. <br />
  51. CVV No
  52. <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
  53. </asp:Panel>
  54. </td>
  55. </tr>
  56. <tr>
  57. <td class="style2">
  58. <asp:Panel ID="Panel2" runat="server" CssClass="style2">
  59. Bank
  60. <asp:DropDownList ID="DropDownList3" runat="server">
  61. <asp:ListItem>SBI</asp:ListItem>
  62. <asp:ListItem>IOB</asp:ListItem>
  63. </asp:DropDownList>
  64. <br />
  65. Card No
  66. <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
  67. </asp:Panel>
  68. </td>
  69. </tr>
  70. <tr>
  71. <td class="style2">
  72. <asp:Panel ID="Panel3" runat="server" CssClass="style2">
  73. Bank
  74. <asp:DropDownList ID="DropDownList4" runat="server">
  75. <asp:ListItem>SBI</asp:ListItem>
  76. <asp:ListItem>IOB</asp:ListItem>
  77. <asp:ListItem>CBI</asp:ListItem>
  78. <asp:ListItem>PNB</asp:ListItem>
  79. <asp:ListItem>HDFC</asp:ListItem>
  80. </asp:DropDownList>
  81. </asp:Panel>
  82. </td>
  83. </tr>
  84. <tr>
  85. <td class="style2">
  86. <asp:Panel ID="Panel4" runat="server" CssClass="style2">
  87. Pay Bill on Delivery Time
  88. <asp:Button ID="Button5" runat="server" Text="I Agree" />
  89. </asp:Panel>
  90. </td> </tr></table>
  91. </div>
  92. </form>
  93. </body>
  94. </html>

The design looks like Figure 1.

Panel Control In ASP.NET
Figure 1

code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class Default3 : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. Panel1.Visible = false;
  12. Panel2.Visible = false;
  13. Panel3.Visible = false;
  14. Panel4.Visible = false;
  15. }
  16. protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
  17. {
  18. if (RadioButtonList1.SelectedIndex == 0)
  19. {
  20. Panel1.Visible = true;
  21. }
  22. if (RadioButtonList1.SelectedIndex == 1)
  23. {
  24. Panel2.Visible = true;
  25. }
  26. if (RadioButtonList1.SelectedIndex == 2)
  27. {
  28. Panel3.Visible = true;
  29. }
  30. if (RadioButtonList1.SelectedIndex == 3)
  31. {
  32. Panel4.Visible = true;
  33. }
  34. }}

Output

The output of the panel in the browser is like figure 2.

Panel Control In ASP.NET
Figure 2

When you click on any option, like Debit Card, the Debit Card Panel appears like Figure 3,

Panel Control In ASP.NET
Figure 3

And when you click on another option like figure 4:

Panel Control In ASP.NET
Figure 4

Summary

In this article, I discussed how we can create a Panel Control in ASP.NET with C#. We saw how with the help of Radio Button, we can control the panel and we can first set the panel invisible and on every radio button, they are seperately visible.