How to get updated Textbox value from Repeater?

丶灬走出姿态 提交于 2019-12-11 10:17:06

问题


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>

回答1:


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
    }
}


来源:https://stackoverflow.com/questions/13648037/how-to-get-updated-textbox-value-from-repeater

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!