Search code examples
c#asp.nettextboxupdatepanel

Full postback on select & remove within update panel


I'm having issues with the asp.net textbox within an update panel. It works perfectly fine when adding or removing each individual character but if I highlight all of the text within the textbox, and then remove it a full postback occurs, not a partial postback which is expected.

Why is this happening? I haven't found anything related to this particular problem so it's likely I'm doing something wrong.

Example aspx:

<asp:UpdatePanel ID="updExample" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
  <ContentTemplate>
    <asp:Repeater ID="rptExample" runat="server" .... >
      <ItemTemplate>
        <asp:TextBox ID="txtExample" runat="server" ClientIDMode="static" Text='<%# Eval("Example") %>' OnTextChanged="txtExample_TextChanged" AutoPostBack="true"></asp:TextBox>
      </ItemTemplate>
    </asp:Repeater>
  </ContentTemplate>
</asp:UpdatePanel>

Example TextChanged Event:

protected void txtExample_TextChanged(object sender, EventArgs e)
{
   updExample.Update();
}

Additional Notes:

  • Switching UpdateMode to 'Always' doesn't work.

Solution

  • Karthikeyan Nagaraj pointed out in the comments to try adding triggers alongside what I already had. I did in fact already have this, however, I was assigning the trigger in the ItemDataBound event which I realized was incorrect after reinvestigating. The ItemCreated event was much more suited.

    I had no issues finding the control in the ItemCreated event, however adding a new async postback trigger to the update panel gave me grief and said the control couldn't be found when changing the text. To resolve this, I used the script managers RegisterAsyncPostBackControl(); method as shown below.

    protected void rptExample_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
       var input = e.item.FindControl("txtExample");
    
       if (input != null) {
         ScriptManager sm = ScriptManager.GetCurrent(this);
         sm.RegisterAsyncPostBackControl(input);
       }
    }