Solved

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

Posted on 2016-08-27
2
45 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now