Hi Tevin,
Check this example. Now please take its reference and correct your code.
HTML
<form id="form1" runat="server">
<asp:RadioButtonList runat="server" ID="rblItems" AutoPostBack="true" OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Text="Name" Value="Name" />
<asp:ListItem Text="Country" Value="Country" />
</asp:RadioButtonList>
<hr />
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table>
<tr>
<th>Id</th>
<th id="thName" runat="server">Name</th>
<th id="thCountry" runat="server">Country</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Id") %>' />
</td>
<td id="tdName" runat="server">
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' /></td>
<td id="tdCountry" runat="server">
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
Namespaces
C#
Using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
BindRepeater();
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (rblItems.SelectedIndex == -1)
{
if (e.Item.ItemType == ListItemType.Header)
{
e.Item.FindControl("thName") as HtmlTableCell.Visible = false;
e.Item.FindControl("thCountry") as HtmlTableCell.Visible = false;
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.FindControl("tdName") as HtmlTableCell.Visible = false;
e.Item.FindControl("tdCountry") as HtmlTableCell.Visible = false;
}
}
if (rblItems.SelectedIndex > -1)
{
if (e.Item.ItemType == ListItemType.Header)
{
e.Item.FindControl("thName") as HtmlTableCell.Visible = rblItems.SelectedValue == "Name" ? false : true;
e.Item.FindControl("thCountry") as HtmlTableCell.Visible = rblItems.SelectedValue == "Country" ? false : true;
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.FindControl("tdName") as HtmlTableCell.Visible = rblItems.SelectedValue == "Name" ? false : true;
e.Item.FindControl("tdCountry") as HtmlTableCell.Visible = rblItems.SelectedValue == "Country" ? false : true;
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
BindRepeater();
}
private void BindRepeater()
{
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(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindRepeater()
End If
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If rblItems.SelectedIndex = -1 Then
If e.Item.ItemType = ListItemType.Header Then
TryCast(e.Item.FindControl("thName"), HtmlTableCell).Visible = False
TryCast(e.Item.FindControl("thCountry"), HtmlTableCell).Visible = False
End If
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
TryCast(e.Item.FindControl("tdName"), HtmlTableCell).Visible = False
TryCast(e.Item.FindControl("tdCountry"), HtmlTableCell).Visible = False
End If
End If
If rblItems.SelectedIndex > -1 Then
If e.Item.ItemType = ListItemType.Header Then
TryCast(e.Item.FindControl("thName"), HtmlTableCell).Visible = If(rblItems.SelectedValue = "Name", False, True)
TryCast(e.Item.FindControl("thCountry"), HtmlTableCell).Visible = If(rblItems.SelectedValue = "Country", False, True)
End If
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
TryCast(e.Item.FindControl("tdName"), HtmlTableCell).Visible = If(rblItems.SelectedValue = "Name", False, True)
TryCast(e.Item.FindControl("tdCountry"), HtmlTableCell).Visible = If(rblItems.SelectedValue = "Country", False, True)
End If
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
BindRepeater()
End Sub
Private Sub BindRepeater()
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 Sub
Screenshot