rtn
rtn

Reputation: 2034

How to prevent page refresh when using jQuery in asp.net

I am trying to implement some jQuery into my asp project and every time a function is triggered is causes the page to refresh back to its initial state. I can see the results triggered by the jQuery for a split second then the page is refreshing so I know that the jQuery is fine.

Is there a way in which this can be stopped. I have tried searching around but there is noting that really explains how to do this and I am realitvly new to the world of asp .net.

I was thinking that I somehow need to use .preventDefault() but have not managed to string anything together.

Hers my code is basically puts the value of option into textbox and shows textbox when select is clicked

 <select id="ops">
   <option>Choose Property</option>
   <option value="one">one</option>
   <option value="two">two</option> 
  </select>
 <asp:Button ClientIDMode="Static" ID="select" runat="server" Text="Select"/>

<div id="mydiv">
<asp:TextBox ClientIDMode="Static" Enabled="false" ID="textbox" runat="server" CssClass="textEntry" />
</div>



<script type="text/javascript">
       $(document).ready(function () {
           $('#mydiv').hide();
           $('#ops').click(getOps);
       });

       function getOps() {
           var choice = $(this).val();
           $('#select').click(function () {
               $('#mydiv').show();
               $('#textbox').val(choice);
           });
       }

</script>

Upvotes: 0

Views: 2348

Answers (1)

uadnal
uadnal

Reputation: 11435

use event.prevenDefault();

<script type="text/javascript">
       $(document).ready(function () {
           $('#mydiv').hide();
           $('#ops').click(getOps);
       });

       function getOps() {
           var choice = $(this).val();
           $('#select').click(function (event) {
                event.preventDefault(); // stops form sumission
               $('#mydiv').show();
               $('#textbox').val(choice);
           });
       }

</script>

Upvotes: 2

Related Questions