Solved

How to dynamically change databound label's dataitem in asp.net Repeater

Posted on 2016-08-27
2
87 Views
1 Endorsement
Last Modified: 2016-08-28
I have a databound repeater control in an asp.net 4 website.

Here's some of the markup:
 <ItemTemplate>
                                        <tr>
                                            <td>
                                                <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>
                                            </td>

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.
1
Comment
Question by:Al230762
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 41773197
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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;
        Repeater1.DataBind();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title> EE Q_28966066 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <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">
            <ContentTemplate>
                <table>
                    <asp:Repeater ID="Repeater1" runat="server">
                         <ItemTemplate>
                            <tr>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "id") %>
                                </td>
                                <td>
                                    <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
                                </td>
                                <td>
                                    <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>
                                </td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>
                </table>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="chkShowLegalSurname" EventName="CheckedChanged" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Comment

by:Al230762
ID: 41773628
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):

<td>
                                                <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>
                                            </td>
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question