简体   繁体   中英

How to get updated Textbox value from Repeater?

I have a repeater control as listed below. It has a textbox control. When a save button is clicked, I need to get the updated text from the textbox. I have the following code; but it gives me the old value when I take the textbox text.

How can we get the updated text?

Code Behind

    protected void Save_Click(object sender, EventArgs e)
    {

        foreach (RepeaterItem item in repReports.Items )
        {
            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem )
            {

                string updatedEmail = ((TextBox)item.Controls[5]).Text;
                string originalEmail = ((HiddenField)item.Controls[7]).Value;


            }
        }
    }

Control Markup

  <div class="repeaterTableBorder">
                <asp:Repeater ID="repReports" runat="server">
                    <ItemTemplate>
                        <div id="repeaterIdentifier" class="repeaterIdentifier">
                            <div class="reportTitle">
                                <%# Eval("ReportName") + ":"%>
                                <asp:HiddenField ID="hdnLastChangeTime" runat="server" Value= '<%# ((DateTime)Eval("RecordSelectionTime")).ToString("MM/dd/yyyy hh:mm:ss.fff tt")%>' />
                                <asp:HiddenField ID="hdnReportID" runat="server" Value='<%# Eval("ReportTypeCode")%>' />
                            </div>
                            <div class="reportFrequency">
                                 <%# " Frequency - Weekly" %> 
                            </div>
                        </div>
                        <div class="reportContent">
                            <div class="repeaterLine">
                                <asp:TextBox ID="txtEmailRecipients" runat="server" class="textEdit" 
                                    Text='<%# Eval("ExistingRecipients") %>'
                                    TextMode="MultiLine"></asp:TextBox>
                                 <asp:HiddenField ID="hdnOriginalRecipients" runat="server" Value='<%# Eval("ExistingRecipients")%>' />
                            </div>
                        </div>

                    </ItemTemplate>
                </asp:Repeater>
            </div>

I assume that you are binding the Repeater to it's DataSource also on postbacks. You should do that only if(!IsPostBack) . Otherwise the values will be overwritten.

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        // databinding code here
    }
}

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