title: Wiring up JavaScript handlers after a Partial Postback in ASP.NET
body: <p>I am trying to use LinkButtons with the DefaultButton property of the ASP.NET Panel in an UpdatePanel.  I have read and used the various other answers that are around describing the wiring up of the click event so that a full postback is not done instead of a partial postback.  When the page loads, I wire up the .click function of the LinkButton so that the DefaultButton property of the ASP.NET panel will work.</p>
  
  <p>This all works fine, until you bring an UpdatePanel into the mix.  With an UpdatePanel, if there is a partial postback, the script to wire up the .click function is not called in the partial postback, and hitting enter reverts to causing a full submit of the form rather than triggering the LinkButton.</p>
  
  <p>How can I cause javascript to be executed after a partial postback to re-wire up the .click function of the LinkButton?</p>
  
  <p>I have produced a sample page which shows the problem.  There are two alerts showing 1) When the code to hook up the .click function is being called, and 2) When the .click function has been called (this only happens when you hit enter in the textbox after the event has been wired up).  To test this code, type something in the textbox and hit Enter.  The text will be copied to the label control, but "Wiring up Event Click" alert will not be shown.  Add another letter, hit enter again, and you'll get a full postback without the text being copied to the label control (as the LinkButton wasn't called).  Because that was a full postback, the Wiring Up Event Click event will be called again, and the form will work properly the next time again.</p>
  
  <p>This is being done with ASP.NET 3.5.</p>
  
  <p>Test Case Code:</p>
  
  <pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.UI.Page" Theme="" EnableTheming="false" AutoEventWireup="true" %&gt;
  
  &lt;script runat="server"&gt;
      void cmdComplete_Click(object sender, EventArgs e)
      {
          lblOutput.Text = "Complete Pressed: " + txtInput.Text;
      }
  
      void cmdFirstButton_Click(object sender, EventArgs e)
      {
          lblOutput.Text = "First Button Pressed";
      }
  
      protected override void OnLoad(EventArgs e)
      {
          HookupButton(cmdComplete);
      }
  
      void HookupButton(LinkButton button)
      {
          // Use the click event of the LinkButton to trigger the postback (this is used by the .click code below)
          button.OnClientClick = Page.ClientScript.GetPostBackEventReference(button, String.Empty);
  
          // Wire up the .click event of the button to call the onclick function, and prevent a form submit
          string clickString = string.Format(@"
              alert('Wiring up click event'); 
              document.getElementById('{0}').click = function() {{ 
                  alert('Default button pressed'); 
                  document.getElementById('{0}').onclick(); 
              }};", button.ClientID, Page.ClientScript.GetPostBackEventReference(button, ""));
          Page.ClientScript.RegisterStartupScript(button.GetType(), "click_hookup_" + button.ClientID, clickString, true);
      }
  &lt;/script&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;DefaultButton/LinkButton Testing&lt;/title&gt;
      &lt;style type="text/css"&gt;
          a.Button { line-height: 2em; padding: 5px; border: solid 1px #CCC; background-color: #EEE; }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;
          DefaultButton/LinkButton Testing&lt;/h1&gt;
      &lt;form runat="server"&gt;
      &lt;asp:ScriptManager runat="server" /&gt;
      &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
          &lt;ContentTemplate&gt;
              &lt;div style="position: relative"&gt;
                  &lt;fieldset&gt;
                      &lt;legend&gt;Output&lt;/legend&gt;
                      &lt;asp:Label runat="server" ID="lblOutput" /&gt;
                  &lt;/fieldset&gt;
                  &lt;asp:Button runat="server" Text="First Button" ID="cmdFirstButton" OnClick="cmdFirstButton_Click" UseSubmitBehavior="false" /&gt;
                  &lt;asp:Panel ID="Panel1" runat="server" DefaultButton="cmdComplete"&gt;
                      &lt;label&gt;
                          Enter Text:&lt;/label&gt;
                      &lt;asp:TextBox runat="server" ID="txtInput" /&gt;
                      &lt;asp:LinkButton runat="server" CssClass="Button" ID="cmdComplete" OnClick="cmdComplete_Click" Text="Complete" /&gt;
                  &lt;/asp:Panel&gt;
              &lt;/div&gt;
          &lt;/ContentTemplate&gt;
      &lt;/asp:UpdatePanel&gt;
      &lt;asp:Button runat="server" ID="cmdFullPostback" Text="Full Postback" /&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>