title: How to display jquery dialog from codebehind
body: <p>I've searched online for awhile trying to figure out how to dynamically display a jquery dialog from codebehind, and I haven't had much luck so far. </p>
  
  <p>I'm trying to have a jquery dialog box pop up after a user is finished registering for my website and clicks the "create user" button. I only want the dialog to pop up with "Thank you for registering!" if the username the user entered doesn't already exist in the database.</p>
  
  <p>Here's what I have and is not working. I'm able to use jquery with other things, except for this one task. Any help? Thanks so much!</p>
  
  <pre><code>&lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt;
  &lt;/asp:ScriptManager&gt;
  
  
  &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
  &lt;ContentTemplate&gt;
      &lt;asp:Label ID="lblError" ForeColor="Red" runat="server"&gt;&lt;/asp:Label&gt;
  &lt;/ContentTemplate&gt;
  &lt;Triggers&gt;
   &lt;asp:AsyncPostBackTrigger ControlID="CreateUserButton" EventName="Click" /&gt;
  &lt;/Triggers&gt;
  &lt;/asp:UpdatePanel&gt;
  
  &lt;script type="text/javascript"&gt;
      $(function () {
          initializedialog();
      });
      function initializedialog() {
          $("#dialog").dialog({
              autoOpen: false,
              hide: 'blind',
              minHeight: 125,
              maxWidth: 300,
              show: 'blind',
              title: 'Thanks!'
          });
      }
  
      //This function is called from the script injected from code-behind.
      function showDialog(message) {
          $("#dialog").remove();
          $("#dialog").append(message);
          $("#dialog").dialog('open');
      }
  &lt;/script&gt;
  
  &lt;div id="pnlpopup"&gt;
              &lt;p class="submitButton"&gt;
                  &lt;asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" 
                   OnClick="CreateUserButton_Click" 
                      ValidationGroup="RegisterUserValidationGroup" SkinID="btnLoginRegister" 
                      Height="29px" Width="107px" /&gt;
              &lt;/p&gt;
  &lt;/div&gt;
  
  &lt;div id="dialog" style="display: none"&gt;
  
  &lt;asp:Label ID="lblMessage" runat="server" Text="Thank you registering!"&gt;
  &lt;/asp:Label&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>protected void CreateUserButton_Click(object sender, EventArgs e)
      {
          bool bStatus = false;
          DataTable dt = new DataTable();
          string strRedirect = "";
          DataRow dr = null;
  
          //retrieve userInput fields
          string stringUserName = UserName.Text;
          string stringPassword = Password.Text;
          string stringConfirmPassword = ConfirmPassword.Text;
  
          //set database user role to default
          string userRole = "db_datawriter";
  
          //check if username already exists in database
          dr = Data_Access_Management.DataAccess.GetUser(stringUserName);
  
          if (dr != null) // Check if the DataRow returns any data from database
          {
              lblError.Text = "That Username already exists.";
  
              bStatus = true;
          }
  
          if (!bStatus)
          {
              //insert user into user table in database
              Data_Access_Management.DataAccess.InsertUser(stringUserName, stringPassword, userRole);
  
              strRedirect = CommonStrings.SessionLoginPage;
  
              //string script = "$('#dialog').dialog('open');";
              //ClientScript.RegisterStartupScript(GetType(), "alert('foo');", script, true);
  
              StringBuilder sb = new StringBuilder();
              string script = "$(function(){initializedialog();showDialog(\"" + sb.ToString() + "\");});";
              ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "dialog", script, true);
  
              //redirect to contact.aspx
              Response.Redirect(strRedirect);
          }
  
      }
  </code></pre>