In this article I will explain with an example, how to dynamically show and hide column in ASP.Net Repeater control using C# and VB.Net.
The column in Repeater control will be shown or hidden dynamically using a CheckBox in ASP.Net using C# and VB.Net.
HTML Markup
The following HTML Markup consists of an ASP.Net Repeater control with an HTML Table Layout.
Above the Repeater control, there is an ASP.Net CheckBox control which has been assigned OnCheckedChanged event handler and the AutoPostBack property is set to True.
The Country column is wrapped inside IF condition which checks whether the IsChecked variable is True.
The value of the IsChecked variable is set when the CheckBox is checked or unchecked.
C#
<asp:CheckBox ID="chkCountry" Text="Show Hide Country" runat="server" OnCheckedChanged="OnCheckedChanged"
AutoPostBack="true" Checked="true" />
<hr />
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Id
</th>
<th scope="col" style="width: 120px">
Name
</th>
<%if(this.IsChecked){ %>
<th scope="col" style="width: 100px">
Country
</th>
<%} %>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>' />
</td>
<%if(this.IsChecked){ %>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
</td>
<%} %>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
VB.Net
<asp:CheckBox ID="chkCountry" Text="Show Hide Country" runat="server" OnCheckedChanged="OnCheckedChanged"
AutoPostBack="true" Checked="true" />
<hr />
<asp:Repeater ID="rptCustomers" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th scope="col" style="width: 80px">
Id
</th>
<th scope="col" style="width: 120px">
Name
</th>
<%If Me.IsChecked Then%>
<th scope="col" style="width: 100px">
Country
</th>
<%End If%>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td>
<asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>' />
</td>
<%If Me.IsChecked Then%>
<td>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
</td>
<%End If%>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the ASP.Net Repeater control
The Repeater is populated with a dynamic DataTable with some dummy data inside the Page Load event handler.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
rptCustomers.DataSource = dt
rptCustomers.DataBind()
End If
End Sub
Dynamically Showing Hiding column in ASP.Net Repeater control
Inside the OnCheckChanged event handler, the third column i.e. Country column of the Repeater control is made visible or hidden based on the CheckBox is checked or unchecked respectively by setting the IsChecked variable.
C#
protected bool IsChecked = true;
protected void OnCheckedChanged(object sender, EventArgs e)
{
this.IsChecked = (sender as CheckBox).Checked;
}
VB.Net
Protected IsChecked As Boolean = True
Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.IsChecked = CType(sender, CheckBox).Checked
End Sub
Screenshot
Demo
Downloads