Dynamically Show Hide Multiple columns in ASP.Net Repeater control using C# and VB.Net

Last Reply one month ago By pandeyism

Posted one month ago

Dynamically Show Hide Multiple column in ASP.Net Repeater control using C# and VB.Net

How to apply on multiple column

Dynamically Show Hide column in ASP.Net Repeater control using C# and VB.Net

Posted one month ago

Hi Faizal,

Refer below sample.

HTML

<asp:CheckBoxList runat="server" ID="chekboxlist1" OnSelectedIndexChanged="OnCheckedChanged"
    AutoPostBack="true" RepeatDirection="Horizontal">
    <asp:ListItem Value="Country" Text="Show Hide Country" Selected="True" />
    <asp:ListItem Value="Name" Text="Show Hide Name" Selected="True" />
</asp:CheckBoxList>
<hr />
<asp:Repeater ID="rptCustomers" runat="server">
    <HeaderTemplate>
        <table cellspacing="0" rules="all" border="1">
            <tr>
                <th scope="col" style="width: 80px">
                    Id
                </th>
                <%if (this.IsCheckedName)
                    { %>
                <th scope="col" style="width: 120px">
                    Name
                </th>
                <%} %>
                <%if (this.IsCheckedCountry)
                    { %>
                <th scope="col" style="width: 100px">
                    Country
                </th>
                <%} %>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Id") %>' />
            </td>
            <%if (this.IsCheckedName)
                { %>
            <td>
                <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>' />
            </td>
            <%} %>
            <%if (this.IsCheckedCountry)
                { %>
            <td>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
            </td>
            <%} %>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

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();
    }
}

protected bool IsCheckedCountry = true;
protected bool IsCheckedName = true;
protected void OnCheckedChanged(object sender, EventArgs e)
{
    foreach (ListItem item in chekboxlist1.Items)
    {
        if (item.Value == "Country")
        {
            this.IsCheckedCountry = item.Selected;
        }
        if (item.Value == "Name")
        {
            this.IsCheckedName = item.Selected;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = 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

Protected IsCheckedCountry As Boolean = True
Protected IsCheckedName As Boolean = True
Protected Sub OnCheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
    For Each item As ListItem In chekboxlist1.Items
        If item.Value = "Country" Then
            Me.IsCheckedCountry = item.Selected
        End If
        If item.Value = "Name" Then
            Me.IsCheckedName = item.Selected
        End If
    Next
End Sub

Screenshot