title: Asp.NET: How to keep focus on textbox in a nested Update Panel
body: <p>I am working on making a semi simple post and reply/forum pages in asp.net(with C#). Everything works however when I went to add update panels it makes me want to throw my head into a wall.</p>
  
  <p>I use a DataList to display the posts. I use a form consisting of two textboxes and a button to insert a new post. One textbox if for the name, and the other for the message.</p>
  
  <p>First update panel I added (nested) is to provide a character count for the post. I have a label in the Content and it is triggered by the textboxes textchanged event. The textbox 'txtMessage' also has a java-script function run 'onkeyup' to keep the focus on the textbox when typing. I limit the characters at 1000.</p>
  
  <p>The next update is to surround the DataList so that it does not post back everytime (if not used and the back button is hit it will go back and visually remove each post which is not a good design practice). However when I just put the panel around the DataList it did not postback the insert form so the boxes were not cleared. Which I would like to be done, so I wrapped everything then by this updatepanel, which then made the character count update panel nested by this one. This now works, but the focus is taken off of the txtMessage box each time the textchanged event fires. So the JavaScript is not firing now? </p>
  
  <p>I have moved the opening and closing of the update panel countless times and have tried different fixes, so any further suggestions would help. The code is below.</p>
  
  <h1>ForumT.aspx</h1>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ForumT.aspx.cs" Inherits="UPE_Site_v1.ForumT" %&gt;
  
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="title" runat="server"&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="headPlaceHolder" runat="server"&gt;
  &lt;script type="text/javascript"&gt;
      function reFocus(id) {
          __doPostBack(id, '');
          document.getElementById(id).blur();
          document.getElementById(id).focus();
      }
  &lt;/script&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID="Content3" ContentPlaceHolderID="contentPlaceHolder" runat="server"&gt;
  
  &lt;asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetPosts" TypeName="TimeTrackerRepository" DataObjectTypeName="System.Guid" DeleteMethod="DeletePost"&gt;    &lt;/asp:ObjectDataSource&gt;
  
  
  
  
  &lt;asp:UpdatePanel ID="upDataList" runat="server" UpdateMode="Conditional"&gt;
      &lt;ContentTemplate&gt;
  
          &lt;div&gt;
              &lt;asp:DataList ID="DataList2" runat="server" CellPadding="4" DataSourceID="ObjectDataSource1"
                  ForeColor="#333333" OnItemCommand="DataList2_ItemCommand" OnItemDataBound="DataList2_ItemDataBound"
                  DataKeyField="PostID" OnItemCreated="DataList2_ItemCreated"&gt;
                  &lt;AlternatingItemStyle BackColor="White" /&gt;
                  &lt;FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /&gt;
                  &lt;HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /&gt;
                  &lt;ItemStyle BackColor="#D4EBD4" /&gt;
                  &lt;ItemTemplate&gt;
                      &lt;div class="row"&gt;
                          &lt;div class="col-xs-12 col-sm-6"&gt;
                              Name: &lt;strong&gt;&lt;%# Eval("Name") %&gt;&lt;/strong&gt;
                          &lt;/div&gt;
                          &lt;div class="col-xs-12 col-sm-6"&gt;
                              &lt;%# Eval("TimePosted") %&gt;
                          &lt;/div&gt;
                          &lt;div class="col-xs-12" style="word-break: break-all"&gt;
                              &lt;%# Eval("Message") %&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;br /&gt;
                      &lt;asp:Button ID="btnDelete" CssClass="btn btn-warning" runat="server" Text="Delete" CommandArgument='&lt;%# Eval("PostID") %&gt;' CommandName="DeleteItem" /&gt;
                      &lt;asp:LinkButton CssClass="btn btn-primary" ID="lkbtnFullPost" runat="server" Text="See Full Post" CommandArgument='&lt;%# Eval("PostID") %&gt;' CommandName="FullPost"&gt;&lt;/asp:LinkButton&gt;
                  &lt;/ItemTemplate&gt;
                  &lt;SelectedItemStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /&gt;
              &lt;/asp:DataList&gt;
  
          &lt;/div&gt;
  
          &lt;%--&lt;/ContentTemplate&gt;
                  &lt;Triggers&gt;
                      &lt;asp:AsyncPostBackTrigger ControlID="btnPost" EventName="Click" /&gt;
                  &lt;/Triggers&gt;
      &lt;/asp:UpdatePanel&gt;--%&gt;
  
          &lt;br /&gt;
          &lt;br /&gt;
  
          &lt;div class="row"&gt;
              &lt;div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 col-sm-offset-1 col-md-offset-2 col-lg-offset-3"&gt;
                  &lt;p&gt;Add a post to this forum:&lt;/p&gt;
                  &lt;div class="form-group"&gt;
                      &lt;asp:Label ID="Label1" runat="server" Text="Name: "&gt;&lt;/asp:Label&gt;
                      &lt;asp:TextBox CssClass="form-control" ID="txtName" runat="server"&gt;&lt;/asp:TextBox&gt;
                      &lt;asp:RequiredFieldValidator runat="server" ControlToValidate="txtName"
                          ErrorMessage="This is a required field." ValidationGroup="Application"
                          Display="Dynamic" ForeColor="Red"&gt;
                      &lt;/asp:RequiredFieldValidator&gt;
                  &lt;/div&gt;
  
                  &lt;%--&lt;asp:UpdatePanel ID="upMessage" runat="server" UpdateMode="Conditional"&gt;
                  &lt;ContentTemplate&gt;--%&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;asp:Label ID="Label2" runat="server" Text="Message: "&gt;    &lt;/asp:Label&gt;
                      &lt;asp:TextBox onkeyup="reFocus(this.id);" CssClass="form-control" ID="txtMessage" runat="server" TextMode="MultiLine" Rows="4" OnTextChanged="txtMessage_TextChanged"&gt;&lt;/asp:TextBox&gt;
  
                      &lt;asp:RequiredFieldValidator runat="server" ControlToValidate="txtMessage"
                          ErrorMessage="This is a required field." ValidationGroup="Application"
                          Display="Dynamic" ForeColor="Red"&gt;
                      &lt;/asp:RequiredFieldValidator&gt;
                      &lt;asp:RegularExpressionValidator runat="server" ControlToValidate="txtMessage"
                          ErrorMessage="Character limit is 1000 characters."
                          ValidationGroup="Application" Display="Dynamic" ForeColor="Red"
                          ValidationExpression=".{0,1000}"&gt;
                      &lt;/asp:RegularExpressionValidator&gt;
                  &lt;/div&gt;
  
                  &lt;br /&gt;
                  &lt;%--&lt;/div&gt;
      &lt;/div&gt;--%&gt;
                  &lt;%--&lt;/ContentTemplate&gt;
                  &lt;Triggers&gt;
                      &lt;asp:AsyncPostBackTrigger ControlID="btnPost" EventName="Click" /&gt;
                  &lt;/Triggers&gt;
      &lt;/asp:UpdatePanel&gt;--%&gt;
                  &lt;%--&lt;div class="row"&gt;
      &lt;div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 col-sm-offset-1 col-md-offset-2 col-lg-offset-3"&gt;--%&gt;
                  &lt;asp:UpdatePanel ID="upMessage" runat="server" UpdateMode="Conditional"&gt;
                      &lt;ContentTemplate&gt;
                          &lt;asp:Label ID="lblCharacterCount" runat="server"&gt;0/1000&lt;/asp:Label&gt;
                      &lt;/ContentTemplate&gt;
                      &lt;Triggers&gt;
                          &lt;asp:AsyncPostBackTrigger ControlID="txtMessage" EventName="TextChanged" /&gt;
                      &lt;/Triggers&gt;
                  &lt;/asp:UpdatePanel&gt;
                  &lt;asp:Button ValidationGroup="Application" CssClass="btn btn-default" ID="btnPost" runat="server" Text="POST IT" OnClick="btnPost_Click" /&gt;
                  &lt;asp:Label ID="lblError" runat="server" Text="" CssClas="Error" ForeColor="Red"&gt;&lt;/asp:Label&gt;
  
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;br /&gt;
          &lt;br /&gt;
          &lt;br /&gt;
      &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <h1>ForumT.aspx.cs</h1>
  
  <h2>only including the textchanged event</h2>
  
  <pre><code>protected void txtMessage_TextChanged(object sender, EventArgs e)
      {
  
          lblCharacterCount.Text = txtMessage.Text.Count().ToString() + "/1000";
          if (txtMessage.Text.Count() &gt;= 1000)
          {
              lblCharacterCount.ForeColor = System.Drawing.Color.Red;
          }
          else
          {
              lblCharacterCount.ForeColor = System.Drawing.Color.Black;
          }
      }
  </code></pre>
  
  <p>Sorry for the code being a little sloppy. Also side not, I am using bootstrap so that is what all of the div's are for </p>