简体   繁体   中英

Getting edited data from Repeater control in asp.net

I have a list

List<DashboardFinancialDates> financialDates = new List<DashboardFinancialDates>();

and I bind this list to a repeater as a data source

FinancialDates.DataSource = financialDates;
                FinancialDates.DataBind();

and here is my repeater control

   <asp:Repeater ID="FinancialDates" runat="server">
                                            <HeaderTemplate>
                                                <table class="DashboardConfigTable">
                                                    <thead>
                                                        <tr>
                                                            <th>Month</th>
                                                            <th>Start Date</th>
                                                            <th>End Date</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <tr>
                                                    <td> <%# Eval("Month") %></td>
                                                    <td><input type="text" class="datepicker" value="<%# Eval("StartDate")%>" /></td>
                                                    <td><input type="text" class="datepicker" value="<%# Eval("EndDate")%>" /></td>
                                                </tr>
                                            </ItemTemplate>
                                            <FooterTemplate>
                                                </tbody>
    </table>
                                            </FooterTemplate>
                                        </asp:Repeater>

Now I am making changes via the edit textbox datepicker and I was thinking repeater will automatically modify the datasource with the updated data but that's not happening. Can someone please tell me how I can catch the edited values back from the repeater on page submit.

Thanks

You have to retrieve the data from the Repeater upon postback. For this to work, you need to use ASP.NET controls to display and edit the data in the markup:

<ItemTemplate>
    <tr>
        <td>
            <asp:Label ID="lbl" runat="server" Text='<%# Eval("Month") %>'></asp:Label></td>
        <td>
            <asp:TextBox ID="txtDateFrom" runat="server" CssClass="datepicker" Text='<%# Eval("StartDate")%>' /></td>
        <td>
            <asp:TextBox ID="txtDateTo" runat="server" CssClass="datepicker" Text='<%# Eval("EndDate")%>' /></td>
    </tr>
</ItemTemplate>

Upon postback, you can retrieve the data like this:

private IEnumerable<Tuple<string, DateTime, DateTime>> GetUpdatedItems()
{
    var lst = new List<Tuple<string, DateTime, DateTime>>();
    var items = rpt.Items
        .OfType<RepeaterItem>()
        .Where(x => x.ItemType == ListItemType.Item
                    || x.ItemType == ListItemType.AlternatingItem);
    foreach (var item in items)
    {
        var month = ((Label)item.FindControl("lbl")).Text;
        var dateFromStr = ((TextBox)item.FindControl("txtDateFrom")).Text;
        var dateToStr = ((TextBox)item.FindControl("txtDateTo")).Text;
        var dateFrom = DateTime.Parse(dateFromStr);
        var dateTo = DateTime.Parse(dateToStr);
        lst.Add(Tuple.Create(month, dateFrom, dateTo));
    }
    return lst.ToArray();
}

The code iterates over the Repeater items and retrieves the values of the controls.

If you need an id for the item, you can add a HiddenField to the ItemTemplate and also retrieve it during PostBack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM