Solved

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

Posted on 2016-08-27
2
103 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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