Solved

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

Posted on 2016-08-27
2
73 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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

765 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