Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following aspx/html page, it uses jquery to display a modal form. Every time I click on a jquery modal generated button it does a post back on the form. I would like to stop this. Below is my script

    < script type = "text/javascript" > $(function () {
     $(".addNew").dialog({
         autoOpen: false,
         width: 300,
         height: 300,
         resizeable: false,
         title: 'New Item',
         modal: true,
         close: function (event, ui) {
             event.preventDefault();
             location.reload(false);
             return false;
         },
         buttons: {
             "Add": function (evt) {
                 evt.preventDefault();
                 var name = $("#<%= txtName.ClientID  %>").val();
                 var surname = $("#<%= txtSurname.ClientID %>").val();
                 var age = $("#<%= txtAge.ClientID %>").val();

                 if (Page_ClientValidate("Person")) {
                     $.ajax({
                         type: 'POST',
                         url: 'Dialog.aspx/AddNewItem',
                         data: '{"name":"' + name + '", "surname":"' + surname + '", "age":' + age + '}',
                         contentType: "application/json; charset=utf-8",
                         dataType: "json",
                         success: function (msg) {
                             if (msg.d) {
                                 $("#user").append("<li>" + name + "</li>");
                             }
                         },
                         error: function () {
                             alert("Error! Try again...");
                         }
                     });
                 }
             },
             "Cancel": function (evt) {
                 evt.preventDefault();
                 $(this).dialog("close");
                 return false;

             }
         }
     });

     $("#add").click(function (event) {
         event.preventDefault();
         $(".addNew").dialog("open");
         return false;
     });
 });

<a href="#" id="add">Add New</a>
<form id="form1" runat="server">
<div>

    UserName
        <asp:TextBox ID="UserName" runat="server" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter a username" Display="None" ControlToValidate="UserName" ></asp:RequiredFieldValidator>
 </div>
 <ul id="user">


 </ul>



<div class="addNew" title="Add new Person">
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="Person" />
<table>
    <tr>
        <td>Name</td>
        <td><asp:TextBox ID="txtName" runat="server" /></td>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please enter a name" Display="None" ControlToValidate="txtName" ValidationGroup="Person"></asp:RequiredFieldValidator>
    </tr>
    <tr>
        <td>Surname</td>
        <td><asp:TextBox ID="txtSurname" runat="server" /></td>
    </tr>
    <tr>
        <td>Age</td>
        <td><asp:TextBox ID="txtAge" runat="server" /></td>
    </tr>
</table>
</div>  

</form>
</body>
share|improve this question
    
Is your handler being called at all? –  Ilia G Dec 23 '11 at 22:11
    
The ajax gets called, but when I close the ui modal form, the page reposts. –  Podge Dec 23 '11 at 22:35
    
Probably because you call location.reload on your close event? –  Ilia G Dec 23 '11 at 22:54
    
liho1eye - you were exactly right. I was using some code I cribbed from the web. Please post as an answer and I will accept it. –  Podge Dec 23 '11 at 22:57

1 Answer 1

up vote 0 down vote accepted

Reposing comment as answer:

The issue is that your code is explicitly calling location.reload() inside dialog's close handler. It is not doing anything useful beyond that, so you can probably remove close handler entirely.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.