title: How to avoid page refresh after button click event in asp.net
body: <p>This is the following code the page refreshes as soon as the btninsert click event is completed i want to stop the page refresh after the btninsert is click</p>
  
  <pre><code>&lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
  
      &lt;ContentTemplate&gt;
  
  
          &lt;div style="margin-bottom: 20px; margin-top: 20px;"&gt;&lt;span&gt;&lt;strong style="font-size: large;"&gt;Edit User&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
          &lt;div&gt;
              &lt;span style="float: left; width: 50%;"&gt;&amp;nbsp;&lt;/span&gt; &lt;span style="float: left; width: 50%;"&gt;
                  &lt;span style="width: 100%; float: left; text-align: right;"&gt;
                      &lt;asp:Label ID="lblMessage" runat="server" Text="-"&gt;&lt;/asp:Label&gt;&lt;/span&gt;
              &lt;/span&gt;
          &lt;/div&gt;
  
          &lt;div style="width: 100%; float: left;"&gt;
              &lt;hr /&gt;
          &lt;/div&gt;
  
  
          &lt;div style="width: 816px; margin-left: 5px; margin-top: 20px; height: 463px;"&gt;
  
              &lt;div style="width: 100%; float: left; padding-top: 15px; height: 257px; margin-left: 0px;"&gt;
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;FullName&lt;/span&gt;
                      &lt;span class="simDivide"&gt;
                          &lt;asp:TextBox ID="txtfullname" runat="server" Width="180px"&gt;
                          &lt;/asp:TextBox&gt;
                      &lt;/span&gt;
  
  
                  &lt;/span&gt;
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;&lt;/span&gt;
                      &lt;span class="simDivide"&gt;&lt;/span&gt;
                  &lt;/span&gt;
  
  
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;Username&lt;/span&gt;
                      &lt;span class="simDivide"&gt;
                          &lt;asp:TextBox ID="txtusername" runat="server" Width="180px"&gt;
                          &lt;/asp:TextBox&gt;
                      &lt;/span&gt;
                  &lt;/span&gt;
  
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;&lt;/span&gt;
                      &lt;span class="simDivide"&gt;&lt;/span&gt;
                  &lt;/span&gt;
  
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;Password&lt;/span&gt;
                      &lt;span class="simDivide"&gt;
                          &lt;asp:TextBox ID="txtpassword" runat="server" Width="180px"&gt;
                          &lt;/asp:TextBox&gt;
                      &lt;/span&gt;
                  &lt;/span&gt;
  
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;&lt;/span&gt;
                      &lt;span class="simDivide"&gt;&lt;/span&gt;
                  &lt;/span&gt;
  
  
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;Mobile No
                      &lt;/span&gt;
                      &amp;nbsp;&lt;span class="simDivide"&gt;&lt;asp:TextBox ID="txtmobileno" runat="server" Width="180px"&gt;
                      &lt;/asp:TextBox&gt;
                      &lt;/span&gt;
                  &lt;/span&gt;
  
                  &lt;span class="Divide"&gt;
  
                      &lt;span class="simDivide"&gt;&lt;/span&gt;
                  &lt;/span&gt;
  
  
                  &lt;span class="Divide"&gt;
                      &lt;span class="simDivide1"&gt;Role
                      &lt;/span&gt;
                      &amp;nbsp;&lt;span class="simDivide"&gt;&lt;asp:TextBox ID="txtrole" runat="server" Width="180px"&gt;
                      &lt;/asp:TextBox&gt;
                      &lt;/span&gt;
                  &lt;/span&gt;
  
  
  
                  &lt;script src="jquery-2.0.2.js"&gt;&lt;/script&gt;
  
                  &lt;script language="javascript"&gt;
  
                      function done() {
                          var list = document.getElementById("tid");
                          list.removeChild(list.lastChild);
  
  
                      }
  
  
                      function changecourse(e) {
  
                          var change = document.getElementById('mytext').value;
                          var i = 1;
  
                          mNewObj = document.createElement('div');
                          mNewObj.id = "BOX" + i;
                          mNewObj.style.visibility = "show";
                          mNewObj.innerHTML = change + "&amp;nbsp&lt;a href='#' style='text-decoration: none; color:red' onClick='done()'&gt; x &lt;/a&gt; ";
  
  
                          document.getElementById("tid").appendChild(mNewObj);
                          i++
                          var a = document.getElementById('mytext').selectedIndex;
                          document.getElementById("ContentPlaceHolder1_Hidden1").value = a;
                          //document.getElementById("ContentPlaceHolder1_btninsert").click();
  
                          deleted();
  
  
  
  
                      }
  
                      function yes() {
  
                          $("#ContentPlaceHolder1_btninsert").click();
  
                      }
  
  
                      //function insert() {
  
                      //    $.ajax({
                      //        type: "POST",
                      //        url: "Edituser.aspx.cs/insert",
  
                      //        success: function () { alert('success'); },
                      //        error: function () { alert('error'); }
  
                      //    });
  
  
                      //}
  
  
                      function cancel() {
  
                          var select = document.getElementById('mytext');
                          select.remove(select.selectedIndex);
                      }
  
  
                      function deleted() {
  
                          document.getElementById("mytext").style.display = 'none';
                          document.getElementById("Button1").style.display = 'none';
                          document.getElementById("tid").style.display = 'inline';
                          document.getElementById("mylink").style.display = 'inline';
                      }
  
  
  
                      function showdiv() {
  
                          document.getElementById("mylink").style.display = 'none';
                          document.getElementById("mytext").style.display = 'inline';
                          document.getElementById("Button1").style.display = 'inline';
  
  
                      }
  
  
                  &lt;/script&gt;
  
  
  
              &lt;input id="Hidden1" type="hidden" runat="server" /&gt;
              &lt;/div&gt;
              &lt;asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Style="margin-left: 5px" Text="Edit" Width="39px" /&gt;
              &lt;br&gt;
              &lt;br&gt;
  
              &lt;asp:UpdatePanel runat="server"&gt;
                  &lt;ContentTemplate&gt;
             &amp;nbsp&amp;nbsp&amp;nbsp&amp;nbsp&lt;div id="tid" &gt;
                                 &lt;/div&gt;
              &lt;div id="di"&gt;
  
                  &lt;a id="mylink" onclick="showdiv()"&gt;Add Depot&lt;/a&gt;
  
                  &lt;select id='mytext' name='mytext' style="display: none"&gt;
                      &lt;option&gt;--Select--&lt;/option&gt;
                      &lt;option&gt;Mumbai&lt;/option&gt;
                      &lt;option&gt;Delhi&lt;/option&gt;
                      &lt;option&gt;Banglore&lt;/option&gt;
                      &lt;option&gt;Ahmedabad&lt;/option&gt;
                  &lt;/select&gt;
                 &lt;input type="button" id="Button1" style="display: none" onclick=" changecourse(); yes(); cancel(); return false;" value="add" /&gt;
                  &lt;/div&gt;
              &lt;asp:Button ID="btninsert" runat="server"  Style="display: none" OnClick="btninsert_Click" Text="Insert" ValidationGroup="C" /&gt;
  
                  &lt;/ContentTemplate&gt;
                  &lt;/asp:UpdatePanel&gt;
               &lt;/ContentTemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p></p>
  
  <p>and this is Edit.aspx.cs in which i have method</p>
  
  <pre><code>    protected void btninsert_Click(object sender, EventArgs e)
      {
          string a = Hidden1.Value;
          string UserId = Convert.ToString(Session["LoginId"]);
          con.Open();
          SqlCommand cmd = new SqlCommand("INSERT INTO UserDepot (UserId,DepotId)" +
              "VALUES ('" + UserId + "','" + a + "')", con);
          cmd.ExecuteNonQuery();
          con.Close();
  
  
      }
  </code></pre>