How to dynamically change databound label's dataitem in Repeater

I have a databound repeater control in an 4 website.

Here's some of the markup:
                                                <asp:Label ID="txtSurname" runat="server" CssClass="RepeaterLabel2"
                                                    Enabled="True" Text='<%# DataBinder.Eval(Container.DataItem, "Surname") %>'
                                                    Width="70px" ToolTip='<%# DataBinder.Eval(Container.DataItem, "Surname") %>'></asp:Label>

I need to change the DataItem for the "txtSurname" label dynamically based on user preference so that it might be bound to the "LegalSurname" dataitem if the user requires it. So user checks the "Show legal surname" option on the webpage and the repeater then  refreshes to show legal surname rather than preferred surname - and vice versa.

Can anybody suggest how that might be done?

Many thanks in advance.
Who is Participating?
Robert SchuttSoftware EngineerCommented:
The choice between the 2 fields is not too hard (see line 50 in the code below) but when you say you want the repeater to refresh (and not the rest of the page?) you probably need an UpdatePanel.

Here's a complete example page:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e) {
        DataTable dt;
        if (IsPostBack) {
            dt = (DataTable)Session["dt"];
        } else {
            dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[]{
                new DataColumn("id"),
                new DataColumn("Surname"),
                new DataColumn("FirstName"),
                new DataColumn("LegalSurname")
            dt.Rows.Add(new object[] { 1, "Surname1", "Peter", "LegalSurname1" });
            dt.Rows.Add(new object[] { 2, "Surname2", "Paul", "LegalSurname2" });
            dt.Rows.Add(new object[] { 3, "Surname3", "Mary", "LegalSurname3" });
            Session["dt"] = dt;
        Repeater1.DataSource = dt;
<html xmlns="">
<head runat="server">
    <title> EE Q_28966066 </title>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:CheckBox ID="chkShowLegalSurname" runat="server" AutoPostBack="true" Text="Show legal surname" onclick="changeCheckColor(this)" />
        <hr />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                    <asp:Repeater ID="Repeater1" runat="server">
                                    <%# DataBinder.Eval(Container.DataItem, "id") %>
                                    <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
                                    <asp:Label ID="txtSurname" runat="server" CssClass="RepeaterLabel2"
                                        Enabled="True" Text='<%# DataBinder.Eval(Container.DataItem, chkShowLegalSurname.Checked ? "LegalSurname" : "Surname") %>'
                                        Width="70px" ToolTip='<%# DataBinder.Eval(Container.DataItem, chkShowLegalSurname.Checked ? "LegalSurname" : "Surname") %>'></asp:Label>
                <asp:AsyncPostBackTrigger ControlID="chkShowLegalSurname" EventName="CheckedChanged" />

Open in new window

Al230762Author Commented:
Many thanks Robert. That's the solution.
I was looking for something far too complicated!
Actually we have it all inside an Update panel so the refreshing is fine.
Here's the actual code I finally used (we're working in VB):

                                                <asp:Label ID="txtSurname" runat="server" CssClass="RepeaterLabel2"
                                                    Enabled="True" Text='<%# If(chkPreferredNames.Checked, DataBinder.Eval(Container.DataItem, "PreferredSurname"), DataBinder.Eval(Container.DataItem, "Surname")) %>'
                                                     Width="70px" ToolTip='<%# DataBinder.Eval(Container.DataItem, "Surname") %>'></asp:Label>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.