Search code examples
c#asp.netrepeater

Edit Textbox in Repeater


I have a repeater with a textbox inside. I am trying to edit the information inside the textbox, retrieve the new data, and write to the DB. With my code its giving me the original info that was in the box. Not the new information that I have added. Here is my code

html:

<asp:LinkButton id="saveReviewLinkButton" text="Save" runat="server" onCommand="saveReviewLinkButton_OnCommand" />
<table>
 <asp:Repeater id="ReviewRepeater" runat="server" onItemDataBound="ReviewRepeater_ItemDataBound">
            <itemtemplate>
              <tr >
                <td ><asp:TextBox id="titleLabel" runat="server" width="200px" textMode="MultiLine"/></td>
              </tr>
            </itemtemplate>
</table>

c#:

protected void ReviewRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
  if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
      Review review = (Review)e.Item.DataItem;
      TextBox titleLabel = (TextBox)e.Item.FindControl("titleLabel");
      titleLabel.Text = review.Title;
    }
}



  protected void saveReviewLinkButton_OnCommand(object sender, EventArgs e)
  {
     TextBox titleLabel = new TextBox();

     foreach (RepeaterItem dataItem in ReviewRepeater.Items)
       {
          titleLabel = (TextBox)dataItem.FindControl("titleLabel");
          string newInfo = titleLabel.Text;
       }
  }

Solution

  • Please make sure, you are binding the data to the repeater by checking in page load

    if(!IsPostBack)
      BindData();